列表
创建普通列表,见上一个for循环的例子。
创建混合类型的列表,就是每个元素可以不是相同类型的数据,也可以列表套列表
创建空列表
>>> li=[]
>>> li
[]
想列表添加元素
append()
>>> li.append('misszhou')
>>> li
['misszhou']
>>> li.append('zyj','2013')
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
li.append('zyj','2013')
TypeError: append() takes exactly one argument (2 given)
extend()
>>> li.extend('zyj','21')
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
li.extend('zyj','21')
TypeError: extend() takes exactly one argument (2 given)
>>> li.extend(['zyj','21'])
>>> li
['misszhou', 'zyj', '21']
insert()可以不放到最后
>>> li.insert(0,'zz')
>>> li
['zz', 'misszhou', 'zyj', '21']
从列表中获取元素
>>> li[1]
'misszhou'
>>> temp=li[0]
>>> li[0]=li[1]
>>> li[1]=temp
>>> li
['misszhou', 'zz', 'zyj', '21']
从列表删除元素
remove()
>>> li.remove('21')
>>> li
['misszhou', 'zz', 'zyj']
>>> li.remove('xx')
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
li.remove('xx')
ValueError: list.remove(x): x not in list
del
>>> del li[0]
>>> li
['zz', 'zyj']
pop()
>>> li
['zyj', '21', 'misszhou']
>>> li.pop()
'misszhou'
>>> li
['zyj', '21']
>>> li.pop(0)
'zyj'
>>> li
['21']
列表分片 Slice
一次性获得多个多个元素,只是获得分片的拷贝,不改变原来的列表
>>> li=['zyj','21','misszhou']
>>> li[0:1]
['zyj']
>>> li
['zyj', '21', 'misszhou']
>>> li[1:]
['21', 'misszhou']
>>> li[:2]
['zyj', '21']
>>> li[:]
['zyj', '21', 'misszhou']
列表的常用操作符
比较操作符/逻辑操作符/连接操作符/重复操作符/成员关系操作符
>>> list1=[123]
>>> list2=[234]
>>> list1>list2
False
>>> list1.append(456)
>>> list2.append(123)
>>> list1
[123, 456]
>>> list2
[234, 123]
>>> list1>list2
False
>>> list3=[123,456]
>>> list1==list3
True
>>> list4=list1+list2
>>> list4
[123, 456, 234, 123]
>>> list1.extend(list2)
>>> list1
[123, 456, 234, 123]
>>> list1+'zyj'
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
list1+'zyj'
TypeError: can only concatenate list (not "str") to list
>>> list1*3
[123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123]
>>> list1*=3
>>> list1
[123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123]
>>> list1*=5
>>> list1
[123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123]
>>> 123 in list1
True
>>> 123 not in list1
False
>>> list5=[123,['zyj','21'],456]
>>> 'zyj' in list5
False
>>> 'zyj' in list5[1]
True
>>> list5[1][1]
'21'
列表的函数
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> list1.count(123)
30
>>> list1.index(123)
0
>>> list1.index(123,3,7)
3
>>> list5.reverse()
>>> list5
[456, ['zyj', '21'], 123]
>>> list5.sort()
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
list5.sort()
TypeError: unorderable types: list() < int()
>>> list6=[4,2,1,5,2,6,3,7]
>>> list6.sort()
>>> list6
[1, 2, 2, 3, 4, 5, 6, 7]
>>> list6.sort(reverse=True)
>>> list6
[7, 6, 5, 4, 3, 2, 2, 1]
>>> list7=list6[:]
>>> list8=list6
>>> list7
[7, 6, 5, 4, 3, 2, 2, 1]
>>> list8
[7, 6, 5, 4, 3, 2, 2, 1]
>>> list6.sort()
>>> list6
[1, 2, 2, 3, 4, 5, 6, 7]
>>> list7
[7, 6, 5, 4, 3, 2, 2, 1]
>>> list8
[1, 2, 2, 3, 4, 5, 6, 7]
元组
(有限制的列表,不可改变内容)
区别:
创建与访问
>>> tuple1=(1,2,3,4)
>>> tuple1
(1, 2, 3, 4)
>>> tuple1[:]
(1, 2, 3, 4)
>>> tuple1[1:]
(2, 3, 4)
>>> tuple1[1]=2
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
tuple1[1]=2
TypeError: 'tuple' object does not support item assignment
>>> temp=(1)
>>> temp
1
>>> type(temp)
<class 'int'>
>>> temp2=2,3,4
>>> type(temp2)
<class 'tuple'>
>>> temp=[]
>>> type(temp)
<class 'list'>
>>> temp=()
>>> type(temp)
<class 'tuple'>
>>> temp=(1,)
>>> type(temp)
<class 'tuple'>
>>> temp=1,
>>> type(temp)
<class 'tuple'>
>>> 8*8
64
>>> 8*(8)
64
>>> 8*(8,)
(8, 8, 8, 8, 8, 8, 8, 8)
更新删除
>>> temp=('zyj','21','misszhou')
>>> temp=temp[:2]+('2013',)
>>> temp
('zyj', '21', '2013')
>>> temp=temp[:3]+('misszhou',)
>>> temp
('zyj', '21', '2013', 'misszhou')
>>> del temp
>>> temp
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
temp
NameError: name 'temp' is not defined
操作符
拼接/重复/关系操作符<><=>=/逻辑and or not/成员in no