1、列表list:a=[value1,value2,value3,value4,…]
方法论methods:
list.append(x) #列表追加,等同于a[len(a):] = [x]list.extend(L) #列表加长,等同于a[len(a):] = Llist.insert(i, x) #列表插入,a.insert(len(a), x)等同于a.append(x)list.remove(x) #列表删除,从first查找value=xlist.pop([i]) #列表剔除并返回值,默认a.pop()是remove the last onelist.index(x) #返回value=x的索引号list.count(x) #返回value=x出现的次数list.sort(cmp=None, key=None, reverse=False) #等同于sorted(iterable[, cmp[, key[, reverse]]])函数list.reverse() #列表反向
应用论using:
1、堆栈Stacks>>> stack = [3, 4, 5]>>> stack.append(6)>>> stack.append(7)>>> stack
[3, 4, 5, 6, 7]>>> stack.pop()7
>>> stack
[3, 4, 5, 6]>>> stack.pop()6
>>> stack.pop()5
>>> stack
[3, 4]
2、队列Queues>>> from collections import deque>>> queue = deque(["Eric", "John", "Michael"])>>> queue.append("Terry") # Terry arrives>>> queue.append("Graham") # Graham arrives>>> queue.popleft() # The first to arrive now leaves'Eric'>>> queue.popleft() # The second to arrive now leaves'John'>>> queue # Remaining queue in order of arrivaldeque(['Michael', 'Terry', 'Graham'])
工具论tools:
filter(function, sequence) #返回sequence中符合function的值>>> def f(x): return x % 3 == 0 or x % 5 == 0
...>>> filter(f, range(2, 25))
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]
map(function, sequence) #返回一个由function生成的列表>>> def cube(x): return x*x*x
...>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
reduce(function, sequence) #返回sequence前两个items在function中值>>> def add(x,y): return x+y
...>>> reduce(add, range(1, 11))55
##如果sequence中只有一个值,返回之>>> def sum(seq):
... def add(x,y): return x+y
... return reduce(add, seq, 0)
...>>> sum(range(1, 11))55
>>> sum([])
0
另类函数del a[index1:index2]
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]>>> del a[0]>>> a
[1, 66.25, 333, 333, 1234.5]>>> del a[2:4]>>> a
[1, 66.25, 1234.5]>>> del a[:]>>> a
[]>>> del a>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'a' is not defined#### 列表a已被清除
2、元组(Tuples)和序列(Sequences )
>>> t = 12345, 54321, 'hello!'>>> t[0]12345
>>> t
(12345, 54321, 'hello!')>>> # Tuples may be nested:... u = t, (1, 2, 3, 4, 5)>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))###元组在输出时总有括号,输入时可有可无。元组可用于表示坐标点,员工记录;元组就像字符串,不可更改,不可单独赋值
##包含0个或一个元素的元组>>> empty = ()>>> singleton = 'hello', # <-- note trailing comma>>> len(empty)
0>>> len(singleton)1
>>> singleton
('hello',)
3、字符集sets
无序的,没有重复的字符集合。创建字符集用set()函数,而不是set{}
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']>>> fruit = set(basket) # create a set without duplicates>>> fruit
set(['orange', 'pear', 'apple', 'banana'])>>> 'orange' in fruit # fast membership testingTrue>>> 'crabgrass' in fruit
False>>> # Demonstrate set operations on unique letters from two words...>>> a = set('abracadabra')>>> b = set('alacazam')>>> a # unique letters in aset(['a', 'r', 'b', 'c', 'd'])>>> a - b # letters in a but not in bset(['r', 'd', 'b'])>>> a | b # letters in either a or bset(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])>>> a & b # letters in both a and bset(['a', 'c'])>>> a ^ b # letters in a or b but not bothset(['r', 'd', 'b', 'm', 'z', 'l'])
4、终于轮到字典了
>>> tel = {'jack': 4098, 'sape': 4139}>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}>>> tel['jack']4098
>>> del tel['sape']>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}>>> tel.keys()
['guido', 'irv', 'jack']>>> tel.has_key('guido')
True###链表中存储关键字-值对元组的话,字典可以从中直接构造。关键字-值对来自一个模式时,可以用链表推导式简单的表达关键字-值链表。>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}>>> dict([(x, x**2) for x in vec]) # use a list comprehension{2: 4, 4: 16, 6: 36}