摘要：本文主要为大家讲解在 Python 开发中常见的几种数据结构。

## 数据结构和序列

### 元组

``In [1]: tup = 4, 5, 6``

``````In [3]: nested_tup = (4, 5, 6), (7, 8)
In [4]: nested_tup
Out[4]: ((4, 5, 6), (7, 8))``````

``````In [5]: tuple([4, 0, 2])
Out[5]: (4, 0, 2)
In [6]: tup = tuple('string')
In [7]: tup
Out[7]: ('s', 't', 'r', 'i', 'n', 'g')``````

``````In [8]: tup[0]
Out[8]: 's'``````

``````In [11]: tup[1].append(3)
In [12]: tup
Out[12]: ('foo', [1, 2, 3], True)``````

``````In [13]: (4, None, 'foo') + (6, 0) + ('bar',)
Out[13]: (4, None, 'foo', 6, 0, 'bar')``````

``````In [14]: ('foo', 'bar') * 4
Out[14]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')``````

### 拆分元组

``````In [15]: tup = (4, 5, 6)
In [16]: a, b, c = tup
In [17]: b
Out[17]: 5``````

``````In [18]: tup = 4, 5, (6, 7)
In [19]: a, b, (c, d) = tup
In [20]: d
Out[20]: 7``````

``````tmp = a
a = b
b = tmp``````

``````In [21]: a, b = 1, 2
In [22]: a
Out[22]: 1
In [23]: b
Out[23]: 2
In [24]: b, a = a, b
In [25]: a
Out[25]: 2
In [26]: b
Out[26]: 1``````

``````In [27]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
In [28]: for a, b, c in seq:
....: print('a={0}, b={1}, c={2}'.format(a, b, c))
a=1, b=2, c=3
a=4, b=5, c=6
a=7, b=8, c=9``````

Python 最近新增了更多高级的元组拆分功能，允许从元组的开头 “摘取” 几个元素。它使用了特殊的语法 *rest ，抓取剩余的部分组成列表：

``````In [29]: values = 1, 2, 3, 4, 5
In [30]: a, b, *rest = values
In [31]: a, b
Out[31]: (1, 2)
In [32]: rest
Out[32]: [3, 4, 5]``````

rest 的部分是想要舍弃的部分，rest 的名字不重要。作为惯用写法，许多 Python 程序员会将不需要的变量使用下划线：

``In [33]: a, b, *_ = values``

### tuple 方法

``````In [34]: a = (1, 2, 2, 2, 3, 4, 2)
In [35]: a.count(2)
Out[35]: 4``````

### 列表

``````In [37]: tup = ('foo', 'bar', 'baz')
In [38]: b_list = list(tup)
In [39]: b_list
Out[39]: ['foo', 'bar', 'baz']``````

list 函数常用来在数据处理中实体化迭代器或生成器：

``````In [42]: gen = range(10)
In [43]: gen
Out[43]: range(0, 10)
In [44]: list(gen)
Out[44]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]``````

### 添加和删除元素

``````In [45]: b_list.append('dwarf')
In [46]: b_list
Out[46]: ['foo', 'peekaboo', 'baz', 'dwarf']``````

insert 可以在特定的位置插入元素

``````In [47]: b_list.insert(1, 'red')
In [48]: b_list
Out[48]: ['foo', 'red', 'peekaboo', 'baz', 'dwarf']``````

insert 的逆运算是 pop，它移除并返回指定位置的元素 **：

``````In [49]: b_list.pop(2)
Out[49]: 'peekaboo'
In [50]: b_list
Out[50]: ['foo', 'red', 'baz', 'dwarf']``````

``````In [51]: b_list.append('foo')
In [52]: b_list
Out[52]: ['foo', 'red', 'baz', 'dwarf', 'foo']
In [53]: b_list.remove('foo')
In [54]: b_list
Out[54]: ['red', 'baz', 'dwarf', 'foo']``````

``````In [55]: 'dwarf' in b_list
Out[55]: True``````

``````In [56]: 'dwarf' not in b_list
Out[56]: False``````

### 串联和组合列表

``````In [57]: [4, None, 'foo'] + [7, 8, (2, 3)]
Out[57]: [4, None, 'foo', 7, 8, (2, 3)]``````

``````In [58]: x = [4, None, 'foo']
In [59]: x.extend([7, 8, (2, 3)])
In [60]: x
Out[60]: [4, None, 'foo', 7, 8, (2, 3)]``````

``````everything = []
for chunk in list_of_lists:
everything.extend(chunk)``````

``````everything = []
for chunk in list_of_lists:
everything = everything + chunk``````

### 排序

``````In [61]: a = [7, 2, 5, 1, 3]
In [62]: a.sort()
In [63]: a
Out[63]: [1, 2, 3, 5, 7]``````

sort 有一些选项，有时会很好用。其中之一是二级排序 key，可以用这个 key 进行排序。例如，我们可以按长度对字符串进行排序：

``````In [64]: b = ['saw', 'small', 'He', 'foxes', 'six']
In [65]: b.sort(key=len)
In [66]: b
Out[66]: ['He', 'saw', 'six', 'small', 'foxes']``````

### 二分搜索和维护已排序的列表

bisect 模块支持二分查找，和向已排序的列表插入值。

• bisect.bisect 可以找到插入值后仍保证排序的位置，
• bisect.insort 是向这个位置插入值：
``````In [67]: import bisect
In [68]: c = [1, 2, 2, 2, 3, 4, 7]
In [69]: bisect.bisect(c, 2)
Out[69]: 4
In [70]: bisect.bisect(c, 5)
Out[70]: 6
In [71]: bisect.insort(c, 6)
In [72]: c
Out[72]: [1, 2, 2, 2, 3, 4, 6, 7]``````

### 切片

``````In [73]: seq = [7, 2, 3, 7, 5, 6, 0, 1]
In [74]: seq[1:5]
Out[74]: [2, 3, 7, 5]``````

``````In [75]: seq[3:4] = [6, 3]
In [76]: seq
Out[76]: [7, 2, 3, 6, 3, 5, 6, 0, 1]``````

``````In [81]: seq[::2]
Out[81]: [7, 3, 3, 6, 1]``````

``````In [82]: seq[::-1]
Out[82]: [1, 0, 6, 5, 3, 6, 3, 2, 7]``````

## 序列函数

### enumerate 函数

``````i = 0
for value in collection:
# do something with value
i += 1``````

Python 内建了一个 enumerate 函数，可以返回 (i, value) 元组序列：

``````for i, value in enumerate(collection):
# do something with value``````

``````In [83]: some_list = ['foo', 'bar', 'baz']
In [84]: mapping = {}
# 同时列出序号和数据内容
In [85]: for i, v in enumerate(some_list):
....:     mapping[v] = i
In [86]: mapping
Out[86]: {'bar': 1, 'baz': 2, 'foo': 0}``````

### sorted 函数

sorted 函数可以从任意序列的元素返回一个新的排好序的列表：

``````In [87]: sorted([7, 1, 2, 6, 0, 3, 2])
Out[87]: [0, 1, 2, 2, 3, 6, 7]
In [88]: sorted('horse race')
Out[88]: [' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']``````

sorted 函数可以接受和 sort 相同的参数。

### zip 函数

zip 可以将多个列表、元组或其它序列成对组合成一个元组列表：

``````In [89]: seq1 = ['foo', 'bar', 'baz']
In [90]: seq2 = ['one', 'two', 'three']
In [91]: zipped = zip(seq1, seq2)
In [92]: list(zipped)
Out[92]: [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]``````

zip 可以处理任意多的序列，元素的个数取决于最短的序列

``````In [93]: seq3 = [False, True]
In [94]: list(zip(seq1, seq2, seq3))
Out[94]: [('foo', 'one', False), ('bar', 'two', True)]``````

zip 的常见用法之一是同时迭代多个序列，可能结合 enumerate 使用：

``````In [95]: for i, (a, b) in enumerate(zip(seq1, seq2)):
....: print('{0}: {1}, {2}'.format(i, a, b))
....:
0: foo, one
1: bar, two
2: baz, three``````

In [96]: pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),

....: ('Schilling', 'Curt')]

In [97]: first_names, last_names = zip(*pitchers)

In [98]: first_names

Out[98]: ('Nolan', 'Roger', 'Schilling')

In [99]: last_names

Out[99]: ('Ryan', 'Clemens', 'Curt')

### reversed 函数

reversed 可以从后向前迭代一个序列：

``````In [100]: list(reversed(range(10)))
Out[100]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]``````

## 字典

### 创建字典

``````In [101]: empty_dict = {}
In [102]: d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
In [103]: d1
Out[103]: {'a': 'some value', 'b': [1, 2, 3, 4]}``````

### 访问字典

``````In [104]: d1[7] = 'an integer'
In [105]: d1
Out[105]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
In [106]: d1['b']
Out[106]: [1, 2, 3, 4]``````

``````In [107]: 'b' in d1
Out[107]: True``````

### 删除

``````In [111]: d1
Out[111]:
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
5: 'some value',
'dummy': 'another value'}
In [112]: del d1[5]
In [114]: ret = d1.pop('dummy')
In [115]: ret
Out[115]: 'another value'
In [116]: d1
Out[116]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}``````

### keys 和 values

keys 和 values 是字典的键和值的迭代器方法。虽然键值对没有顺序，这两个方法可以用相同的顺序输出键和值

``````In [117]: list(d1.keys())
Out[117]: ['a', 'b', 7]
In [118]: list(d1.values())
Out[118]: ['some value', [1, 2, 3, 4], 'an integer']``````

### 融合

``````In [119]: d1.update({'b' : 'foo', 'c' : 12})
In [120]: d1
Out[120]: {'a': 'some value', 'b': 'foo', 7: 'an integer', 'c': 12}``````

update 方法是原地改变字典，因此任何传递给 update 的键的旧的值都会被舍弃

### 用序列创建字典

``````mapping = {}
for key, value in zip(key_list, value_list):
mapping[key] = value``````

``````In [121]: mapping = dict(zip(range(5), reversed(range(5))))
In [122]: mapping
Out[122]: {0: 4, 1: 3, 2: 2, 3: 1, 4: 0}``````

### 默认值

``````if key in some_dict:
value = some_dict[key]
else:
value = default_value``````

``value = some_dict.get(key, default_value)``

get 默认会返回 None，如果不存在键，pop 会抛出一个例外。关于设定值，常见的情况是在字典的值是属于其它集合，如列表。例如，你可以通过首字母，将一个列表中的单词分类：

``````In [123]: words = ['apple', 'bat', 'bar', 'atom', 'book']
In [124]: by_letter = {}
In [125]: for word in words:
# 取首字母
.....:     letter = word[0]
.....: if letter not in by_letter:
# 没有该首字母，以该首字母为键，word为值
.....: by_letter[letter] = [word]
.....: else:
# 直接添加
.....: by_letter[letter].append(word)
.....:
In [126]: by_letter
Out[126]: {'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}``````

setdefault 方法就正是干这个的。前面的 for 循环可以改写为：

``````for word in words:
letter = word[0]
by_letter.setdefault(letter, []).append(word)``````

collections 模块有一个很有用的类，defaultdict，它可以进一步简化上面。传递类型或函数以生成每个位置的默认值：

``````from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)``````

### 有效的键类型

``````In [127]: hash('string')
Out[127]: 5023931463650008331
In [128]: hash((1, 2, (2, 3)))
Out[128]: 1097636502276347782
In [129]: hash((1, 2, [2, 3])) # fails because lists are mutable
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-800cd14ba8be> in <module>()
----> 1 hash((1, 2, [2, 3])) # fails because lists are mutable
TypeError: unhashable type: 'list'``````

``````In [130]: d = {}
In [131]: d[tuple([1, 2, 3])] = 5
In [132]: d
Out[132]: {(1, 2, 3): 5}``````

## 集合

### 创建

``````In [133]: set([2, 2, 2, 1, 3, 3])
Out[133]: {1, 2, 3}
In [134]: {2, 2, 2, 1, 3, 3}
Out[134]: {1, 2, 3}``````

``````In [135]: a = {1, 2, 3, 4, 5}
In [136]: b = {3, 4, 5, 6, 7, 8}``````

### 合并 union 或者 |

``````In [137]: a.union(b)
Out[137]: {1, 2, 3, 4, 5, 6, 7, 8}
In [138]: a | b
Out[138]: {1, 2, 3, 4, 5, 6, 7, 8}``````

### 交集 intersection 或者 &

``````In [139]: a.intersection(b)
Out[139]: {3, 4, 5}
In [140]: a & b
Out[140]: {3, 4, 5}``````

``````In [141]: c = a.copy()
In [142]: c |= b
In [143]: c
Out[143]: {1, 2, 3, 4, 5, 6, 7, 8}
In [144]: d = a.copy()
In [145]: d &= b
In [146]: d
Out[146]: {3, 4, 5}``````

``````In [147]: my_data = [1, 2, 3, 4]
In [148]: my_set = {tuple(my_data)}
In [149]: my_set
Out[149]: {(1, 2, 3, 4)}``````

### superset 和 subset

``````In [150]: a_set = {1, 2, 3, 4, 5}
In [151]: {1, 2, 3}.issubset(a_set)
Out[151]: True
In [152]: a_set.issuperset({1, 2, 3})
Out[152]: True``````

``````In [153]: {1, 2, 3} == {3, 2, 1}
Out[153]: True``````

## 列表、集合和字典推导式

### 列表推导式！

``[expr for val in collection if condition]``

``````result = []
for val in collection:
if condition:
result.append(expr)``````

filter 条件可以被忽略，只留下表达式就行。例如，给定一个字符串列表，我们可以过滤出长度在 2 及以下的字符串，并将其转换成大写：

``````In [154]: strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
In [155]: [x.upper() for x in strings if len(x) > 2]
Out[155]: ['BAT', 'CAR', 'DOVE', 'PYTHON']``````

### 字典的推导式 ！

``dict_comp = {key-expr : value-expr for value in collection if condition}``

### 集合的推导式！

``set_comp = {expr for value in collection if condition}``

``````In [156]: unique_lengths = {len(x) for x in strings}
In [157]: unique_lengths
Out[157]: {1, 2, 3, 4, 6}``````

map 函数可以进一步简化：

``````In [158]: set(map(len, strings)) # 妙极
Out[158]: {1, 2, 3, 4, 6}``````

``````In [159]: loc_mapping = {val : index for index, val in enumerate(strings)}
In [160]: loc_mapping
Out[160]: {'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}``````

### 嵌套列表推导式

``````In [161]: all_data = [['John', 'Emily', 'Michael', 'Mary', 'Steven'],
.....: ['Maria', 'Juan', 'Javier', 'Natalia', 'Pilar']]``````

``````names_of_interest = []
for names in all_data:
enough_es = [name for name in names if name.count('e') >= 2]
names_of_interest.extend(enough_es)``````

``````In [162]: result = [name for names in all_data for name in names
.....: if name.count('e') >= 2]
In [163]: result
Out[163]: ['Steven']``````

``````In [164]: some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
In [165]: flattened = [x for tup in some_tuples for x in tup]
In [166]: flattened
Out[166]: [1, 2, 3, 4, 5, 6, 7, 8, 9]``````

``````flattened = []
for tup in some_tuples:
for x in tup:
flattened.append(x)``````

``````In [167]: [[x for x in tup] for tup in some_tuples]
Out[167]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]``````