python 链表推导式_五--python之数据结构(Data Structures)
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 "", line 1, in 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}
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的python 链表推导式_五--python之数据结构(Data Structures)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 控制qq_最必要的最小建议
- 下一篇: python爬取豆瓣电影top250_P