一、列表
1. 创建列表
>>> name = ['gao', 'sam', 'snow', 'jey'] >>> type(name) <type 'list'> >>> name=list(['gao', 'sam', 'snow', 'jey']) # python 3.0+ 会报错 >>> type(name) <type 'list'> >>> name = [] >>> type(name) <type 'list'> >>> name = list() # python 3.0+ 会报错 >>> type(name) <type 'list'>
2. 切片 与字符串类似
>>> name = ['Sam', 'Jey', 'Snow', 'Amy'] >>> name[2] 'Snow' >>> name[1:] ['Jey', 'Snow', 'Amy'] >>> name[-2:] ['Snow', 'Amy'] >>> name[-1:-3:-1] ['Amy', 'Snow']
3. 追加
>>> name = ['Sam', 'Jey', 'Snow'] >>> name.append('Yang') >>> name ['Sam', 'Jey', 'Snow', 'Yang']
4. 插入
>>> name ['Sam', 'Jey', 'Snow', 'Yang'] >>> name.insert(1, 'Long') >>> name ['Sam', 'Long', 'Jey', 'Snow', 'Yang']
5. 修改
>>> name ['Sam', 'Long', 'Jey', 'Snow', 'Yang'] >>> name[2]='Goo' >>> name ['Sam', 'Long', 'Goo', 'Snow', 'Yang']
6. 删除
方法一
>>> name ['Sam', 'Long', 'Goo', 'Snow', 'Yang'] >>> del name[0] >>> name ['Long', 'Goo', 'Snow', 'Yang']
方法二
>>> name ['Long', 'Goo', 'Snow', 'Yang'] >>> name.remove('Goo') >>> name ['Long', 'Snow', 'Yang']
方法三
>>> name = ['Sam', 'Jey', 'Snow', 'Yang'] >>> name.pop() 'Yang' >>> name ['Sam', 'Jey', 'Snow'] >>> name.pop(1) 'Jey' >>> name ['Sam', 'Snow']
7. 扩展
>>> name = ['Sam', 'Jey', 'Snow', 'Yang'] >>> girls=['Goo', 'Di'] >>> name.extend(girls) >>> name ['Sam', 'Jey', 'Snow', 'Yang', 'Goo', 'Di']
8. 拷贝
浅拷贝一
>>> name = ['Sam', 'Jey',['age', 'salary']] >>> name2 = name.copy() >>> name2 ['Sam', 'Jey', ['age', 'salary']] >>> name[0]='Sam2' >>> name[2][0]='age2' >>> name ['Sam2', 'Jey', ['age2', 'salary']] >>> name2 ['Sam', 'Jey', ['age2', 'salary']]
浅拷贝二
>>> name = ['Sam', 'Jey',['age', 'salary']] >>> name2=name[:] >>> name2 ['Sam', 'Jey', ['age', 'salary']] >>> name[0]='Sam2' >>> name[2][0]='age2' >>> name ['Sam2', 'Jey', ['age2', 'salary']] >>> name2 ['Sam', 'Jey', ['age2', 'salary']]
浅拷贝三 (只适应python2.0+)
>>> name = ['Sam', 'Jey', ['age', 'year']] >>> name2 = list(name) >>> name2 ['Sam', 'Jey', ['age', 'year']]
深拷贝
>>> import copy >>> name = ['Sam', 'Jey',['age', 'salary']] >>> name2=copy.deepcopy(name) >>> name[0]='Sam2' >>> name[2][0]='age2' >>> name ['Sam2', 'Jey', ['age2', 'salary']] >>> name2 ['Sam', 'Jey', ['age', 'salary']]
9. 统计
>>> name = ['Sam', 'Jey', 'Sam',1, 9] >>> name.count('Sam') 2
10. 排序翻转
>>> name = ['Sam', 'Jey', 'Sam',1, 9] >>> name.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'int' and 'str' >>> name = ['Sam', 'Jey', 'Sam'] >>> name.sort() >>> name ['Jey', 'Sam', 'Sam'] >>> name = ['Sam', 'Jey', 'Sam','1', '9'] >>> name.sort() >>> name ['1', '9', 'Jey', 'Sam', 'Sam'] >>> name.reverse() >>> name ['Sam', 'Sam', 'Jey', '9', '1']
10. 获取下标
>>> name = ['Sam', 'Jey', 'Sam','1', '9'] >>> name.index('Jey') 1 >>> name.index('aa') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'aa' is not in list
11. 清除
>>> name = ['Sam', 'Jey', 'Sam','1', '9'] >>> name.clear() >>> name []
二. 元组
元组就是不可变的列表,只能读取,不能修改
1. 元组定义
>>> name=('Sam', 'Gao', 'jey') >>> name ('Sam', 'Gao', 'jey') >>> team=('ops',) >>> team ('ops',) >>> type(team) <class 'tuple'>
2. 切片 与列表类似