标签:
list是一种有序的集合,可以随时添加和删除其中的元素。
>>> a_list=[] >>> a_list []
>>> a_list=[1,2,3,4,5] >>> a_list [1, 2, 3, 4, 5]
>>> for i in a_list: ... print i ... 1 2 3 4 5
>>> a_list.append(‘adele‘) >>> a_list [1, 2, 3, 4, 5, ‘adele‘]
>>> a_list.insert(1,‘taylor‘) >>> a_list [1, ‘taylor‘, 2, 3, 4, 5, ‘adele‘]
>>> a_list.extend([‘1989‘,‘hello‘]) >>> a_list [1, ‘taylor‘, 2, 3, 4, 5, ‘adele‘, ‘1989‘, ‘hello‘]
>>> a_list.pop() #默认删除最后一个值 ‘hello‘
>>> a_list.pop(1) #指定删除位置 ‘taylor‘
>>> a_list [1, 1, 2, 3, 4, 5, ‘1989‘, ‘adele‘] >>> a_list.remove(1) >>> a_list [1, 2, 3, 4, 5, ‘1989‘, ‘adele‘]
>>> del a_list[0] #删除指定元素 >>> a_list [2, 3, 4, 5, ‘1989‘, ‘adele‘]
>>> del a_list[0:2] #删除连续几个元素 >>> a_list [4, 5, ‘1989‘, ‘adele‘]
>>> del a_list #删除整个list >>> a_list Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ‘a_list‘ is not defined
>>> a_list.sort() >>> a_list [1, 1, 2, 3, 4, 5, ‘1989‘, ‘adele‘]
>>> a_list [1, 2, 3, 4, 5, ‘1989‘, ‘adele‘] >>> a_list.reverse() >>> a_list [‘adele‘, ‘1989‘, 5, 4, 3, 2, 1]
>>> [1,2,3]+[‘a‘,‘b‘,‘c‘] [1, 2, 3, ‘a‘, ‘b‘, ‘c‘]
>>> [‘hello‘]*4 [‘hello‘, ‘hello‘, ‘hello‘, ‘hello‘]
>>> 1 in [1,2,3] True
标签:
原文地址:http://www.cnblogs.com/lilip/p/5554068.html