字典
字典是 Python 的内置数据结构,可以简单理解为键值对,实际开发过程中十分常用。字典有很多创建方法,比如:
>>> a = dict(one=1, two=2, three=3)
>>> b = {'one':1, 'two':2, 'three':3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two',2),('one',1),('three',3)])
>>> e = dict({'three':3, 'one':1, 'two':2})
>>> a == b == c == d == e
True
集合
集合也是 Python 的内置数据结构,其本质是许多唯一对象的聚集。因此,集合可以用于去重,比如:
>>> colors = ['red','yellow','red','yellow','red','blue']
>>> set(colors)
{'yellow', 'red', 'blue'}
本质
字典和集合的背后都因为有了散列表(Hash Table)的支撑而变的性能出众。当然也因此有了天然的劣势,此处暂时按下不表。
(空间换时间)