列表和数组

来源:互联网 发布:国内旅游收入数据统计 编辑:程序博客网 时间:2024/06/11 08:30
list Python 内置单独的一种数据类型是列表,list是一种有序的集合,可以随时添加和删除其中的元素。classmates = ['Michael', 'Bob', 'Tracy']print classmates;C:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py['Michael', 'Bob', 'Tracy']['Michael', 'Bob', 'Tracy', 'Adam']['Michael', 'Sarah', 'Tracy', 'Adam']Process finished with exit code 0list元素也可以是另一个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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py4pythonjava['asp', 'php']schemeaspphpProcess finished with exit code 0tuple另一种有序列表叫元组: tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样列出同学的名字:classmates = ('Michael', 'Bob', 'Tracy')print classmates;classmates[0]='xxyy'print classmates;C:\Python27\python.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 assignmentProcess finished with exit code 1

原创粉丝点击