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