列表(List)、映射(Dict)、集合(Set)是python的三种基本数据结构,日常的工作中需要熟练掌握它们的基本操作函数
List
添加元素
尾部添加元素:append
>>> l = [1,2,3]
>>> l.append(4)
>>> l
[1, 2, 3, 4]
指定位置添加元素:insert(index从0开始)
>>> l = [1,2,3]
>>> l.insert(2, 4)
>>> l
[1, 2, 4, 3]
列表尾部添加另一个列表(extend)
>>> l1 = [1,2,3]
>>> l2 = [4,5,6]
>>> l1.extend(l2)
>>> l1
[1, 2, 3, 4, 5, 6]
将列表元素按某个分隔符拼接到一起(join)
>>> '_'.join(['a', 'b', 'c'])
'a_b_c'
列表推导
遍历列表然后进行一定的变换(过滤、映射)是常见操作,如果先创建一个列表来存放遍历时产生的新元素当然可以,但是代码比较冗余不够"pythonic",比如现在要遍历0-9然后过滤出偶数:
常规写法:
>>> l1 = range(10)
>>> l2 = []
>>> for x in l1:
... if x % 2 == 0:
... l2.append(x)
...
>>> l2
[0, 2, 4, 6, 8]
列表推导写法:
>>> l = range(10)
>>> [x for x in l if x%2 == 0]
[0, 2, 4, 6, 8]
for-loop
遍历列表同时获得对应index
>>> for index, item in enumerate(['a', 'b', 'c']):
... print(index, item)
...
(0, 'a')
(1, 'b')
(2, 'c')
同时遍历两个列表(zip)
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> for x, y in zip(a,b):
... print(x, y)
...
(1, 4)
(2, 5)
(3, 6)
Map
defaultdict
默认给key赋值,在for循环时代码会比较干净
常规写法:
>>> m = {}
>>> if 'a' in m:
... m['a'] += 10
... else:
... m['a'] = 10
...
>>> m
{'a': 10}
defaultdict写法:
>>> from collections import defaultdict
>>> m = defaultdict(lambda: 0)
>>> m['a'] += 10
>>> print(m)
defaultdict(<function <lambda> at 0x109b99668>, {'a': 10})
按值排序
>>> m = {'a': 1, 'b': 3, 'c': 2}
>>> x = sorted(m.items(), key = lambda x: x[1])
>>> x
[('a', 1), ('c', 2), ('b', 3)]
Set
并集
>>> s1 = set(['a', 'b', 'c'])
>>> s2 = set(['a', 'b', 'd'])
>>> s1.union(s2)
set(['a', 'c', 'b', 'd'])
>>> s1 | s2
set(['a', 'c', 'b', 'd'])
交集
>>> s1 = set(['a', 'b', 'c'])
>>> s2 = set(['a', 'b', 'd'])
>>> s1.intersection(s2)
set(['a', 'b'])
>>> s1 & s2
set(['a', 'b'])
差集
>>> s1 = set(['a', 'b', 'c'])
>>> s2 = set(['a', 'b', 'd'])
>>> s1.difference(s2)
set(['c'])
>>> s1 - s2
set(['c'])