【原文链接】
1 字典的dict的定义与特点
- (1)字典也是用花括号括起来的,与集合不同的是字典中的元素都是key-value对
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> type(a)
<class 'dict'>
2 dict 常用的运算
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> a["a"]
1
>>> a["b"]
2
- (2)len() 函数计算字典中key-value对的数量
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> len(a)
4
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> max(a)
'd'
>>> a={"a":1,"b":2,"c":3,"d":4}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> a
{'a': 10, 'b': 2, 'c': 3, 'd': 4}
>>> min(a)
'a'
- (5) in, not in 针对字典的key判断字典中是否有给定的key
{'a': 10, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> "a" in a
True
>>> 10 in a
False
>>> "b" in a
True
>>> 10 not in a
True
3 字典中常用函数
>>> a={"a":1,"b":2}
>>> a
{'a': 1, 'b': 2}
>>> a.clear()
>>> a
{}
>>> a={"a":1,"b":[1,2,3]}
>>> b=a.copy()
>>> a
{'a': 1, 'b': [1, 2, 3]}
>>> b
{'a': 1, 'b': [1, 2, 3]}
>>> a["b"].append(4)
>>> a
{'a': 1, 'b': [1, 2, 3, 4]}
>>> b
{'a': 1, 'b': [1, 2, 3, 4]}
>>> a["a"]=100
>>> a
{'a': 100, 'b': [1, 2, 3, 4]}
>>> b
{'a': 1, 'b': [1, 2, 3, 4]}
- (3)fromkeys(iterable,value=None), 从iterable中的值作为键创建新的字典,每个键对应的值为value,不指定时默认为None
>>> a=[1,2,3,4,5]
>>> b=dict.fromkeys(a)
>>> b
{1: None, 2: None, 3: None, 4: None, 5: None}
>>> c=dict.fromkeys(a,100)
>>> c
{1: 100, 2: 100, 3: 100, 4: 100, 5: 100}
- (4)get(key,default=None),从字典中取给定key对应的值,如果字典中没有此key,则可以通过default给定默认值,如果默认值不给定,则默认为None
>>> a={"a":1,"b":2,"c":3,"d":5}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 5}
>>> b=a.get("a")
>>> b
1
>>> b=a.get("a",10)
>>> b
1
>>> b=a.get("f")
>>> b
>>> b=a.get("f",100)
>>> b
100
- (5)pop(key,default=None) 从字典中弹出给定key对应的值,若key在字典中找不到,则返回default指定的值,如果key在字典中找不到,default也未指定,则报KeyError异常
>>> a={"a":1,"b":2,"c":3,"d":4,"e":5}
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("a")
>>> b
1
>>> a
{'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("f",100)
>>> b
100
>>> a
{'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> b=a.pop("f")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'f'
- (6)update(list or dict), 将一个二元组的列表或者字典更新至现有的字典中
>>> a={"a":1,"b":2}
>>> b={"c":3,"d":4}
>>> a
{'a': 1, 'b': 2}
>>> b
{'c': 3, 'd': 4}
>>> a.update(b)
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> b
{'c': 3, 'd': 4}
>>> c=[(1,2),(3,4)]
>>> a.update(c)
>>> a
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 1: 2, 3: 4}
- (7)popitem() 从字典中弹出一个(key,value)二元组,若字典为空字典,则报KeyError异常
>>> a={1:2,3:4,5:6}
>>> a
{1: 2, 3: 4, 5: 6}
>>> b=a.popitem()
>>> b
(5, 6)
>>> a
{1: 2, 3: 4}
>>> c=a.popitem()
>>> c
(3, 4)
>>> a
{1: 2}
>>> d=a.popitem()
>>> d
(1, 2)
>>> a
{}
>>> e=a.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
- (8)setdefault(key,default=None) 如果字典中不存在key,则将此key default加入字典中,如果字典中存在此key,则将key对应的value返回,否则返回设置的默认值
>>> a={1:2,3:4}
>>> b=a.setdefault(2)
>>> b
>>> a
{1: 2, 3: 4, 2: None}
>>> b=a.setdefault(4,5)
>>> b
5
>>> a
{1: 2, 3: 4, 2: None, 4: 5}
>>> b=a.setdefault(1,100)
>>> b
2
>>> a
{1: 2, 3: 4, 2: None, 4: 5}
- (9)keys() 返回字典key列表的一个对象,可通过list方法转换为key的列表
>>> a={"a":1,"b":2,"c":3}
>>> b=a.keys()
>>> b
dict_keys(['a', 'b', 'c'])
>>> type(b)
<class 'dict_keys'>
>>> list(b)
['a', 'b', 'c']
- (10)values() 返回字典value列表的一个对象,可通过list方法转换为value的列表
>>> a={"a":1,"b":2,"c":3}
>>> b=a.values()
>>> b
dict_values([1, 2, 3])
>>> type(b)
<class 'dict_values'>
>>> list(b)
[1, 2, 3]
- (11)items() 返回字典value-value对二元组的一个对象,可通过list方法转换为key-value的列表
>>> a={"a":1,"b":2,"c":3}
>>> b=a.items()
>>> b
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>> type(b)
<class 'dict_items'>
>>> list(b)
[('a', 1), ('b', 2), ('c', 3)]