zoukankan      html  css  js  c++  java
  • 列表和数组

    list 
    Python 内置单独的一种数据类型是列表,list是一种有序的集合,可以随时添加和删除其中的元素。
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    print len(classmates)
    
    打印数组长度
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    print len(classmates)
    
    print classmates[0];
    print classmates[1];
    print classmates[2];
    print  classmates[3];
    
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a2.py", line 8, in <module>
        print  classmates[3];
    IndexError: list index out of range
    
    数组越界:
    
    list是一个可变的有序表,所以,可以往list中追加元素到末尾:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    classmates.insert(1, 'Jack')
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
    
    
    要删除list末尾的元素,用pop()方法:
    
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    classmates.insert(1, 'Jack')
    print classmates;
    
    print  classmates.pop();
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
    Adam
    
    要删除指定位置的元素,用pop(i)方法,其中i是索引位置:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    classmates.pop(1)
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Tracy', 'Adam']
    
    Process finished with exit code 0
    
    
    要把某个元素替换成别的元素,可以直接赋值给对应的索引位置:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    classmates[1] = 'Sarah'
    print classmates;
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Sarah', 'Tracy', 'Adam']
    
    Process finished with exit code 0
    
    
    list元素也可以是另一个list,比如:(2维数组):
    
    s = ['python', 'java', ['asp', 'php'], 'scheme'];
    print len(s);
    print s[0];
    print s[1];
    print s[2];
    print s[3];
    print s[2][0];
    print s[2][1];
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    4
    python
    java
    ['asp', 'php']
    scheme
    asp
    php
    
    Process finished with exit code 0
    
    tuple
    
    另一种有序列表叫元组: tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样列出同学的名字:
    
    classmates = ('Michael', 'Bob', 'Tracy')
    print classmates;
    
    classmates[0]='xxyy'
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ('Michael', 'Bob', 'Tracy')
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a2.py", line 4, in <module>
        classmates[0]='xxyy'
    TypeError: 'tuple' object does not support item assignment
    
    Process finished with exit code 1
    

  • 相关阅读:
    NOIP2020 退役记
    CSP2020 游记
    Owenのgalgame 颓废合集
    Atcoder Grand Contest 选做
    xlrd》操作excel 出现的问题:File "D:python37libsite-packagesxlrdformula.py", line 1150, in evaluate_name_formula assert len(tgtobj.stack) == 1
    数据库文档生成代码
    vue 父类组件如何引入子组件
    jna设置回调函数接收c++数组
    在一个vue文件调用另外一个vue文件中的方法
    idea 执行sprinboot 报错 commend line is too long
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349627.html
Copyright © 2011-2022 走看看