python数据结构——字典
字典可能是Python最为重要的数据结构。它更为常见的名字是哈希映射或关联数组。它是键值对的大小可变集合,键和值都是Python对象。创建字典的方法之一是使用尖括号,用冒号分隔键和值:
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
可以用del关键字或pop方法(返回值的同时删除键)删除值:
In [108]: d1[5] = 'some value' In [109]: d1 Out[109]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value'} In [110]: d1['dummy'] = 'another value' 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 [113]: d1 Out[113]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 'dummy': 'another value'} 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是字典的键和值的迭代器方法。虽然键值对没有顺序,这两个方法可以用相同的顺序输出键和值:
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']
用update方法可以将一个字典与另一个融合:
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
因为字典本质上是2元元组的集合,dict可以接受2元元组的列表:
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
因此,dict的方法get和pop可以取默认值进行返回,上面的if-else语句可以简写成下面:
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: .....: 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)
有效的键类型
字典的值可以是任意Python对象,而键通常是不可变的标量类型(整数、浮点型、字符串)或元组(元组中的对象必须是不可变的)。这被称为“可哈希性”。可以用hash函数检测一个对象是否是可哈希的(可被用作字典的键):
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}
- 基础篇12-python基本数据结构-字典
- Python数据结构:序列(列表[]、元组())与映射(字典{})语法总结
- Python数据结构:列表、字典、元组、集合
- Python数据结构大合集|元祖、列表、字典和集合详解,助你简明扼要学Python
- Python自学1:Python 基本数据结构:列表、元组、字典
- Python数据结构:序列(列表[]、元组())与映射(字典{})语法总结
- python数据结构之字典
- Python实现列表转换成字典数据结构的方法
- Python数据结构中列表,字典,元组,集合的总结
- Python第五章-内置数据结构04-字典
- python数据结构 - 字典
- Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
- python 字典数据结构
- Python字典集合数据结构深入理解
- Python数据结构:序列(列表[]、元组())与映射(字典{})语法总结
- Python中列表、字典、元组、集合数据结构整理
- Python内存数据结构--字典
- 【Python】11、python内置数据结构之字典
- python数据结构之字典
- python 数据结构--Dictionary(字典)