1、字典,好名字啊,让你忘不了的名字,如同java中的Map,HashMap
2、字典,别称:哈希表、关联数组。英文名:我就不写
3、【增】字典的创建,没错就是使用 {},例子中是空的字典,即没有元素
>>> tempA = {}
4、字典的元素,都是成对的,即key-value,组成为一对
5、字典可以存储任意类型的value,如字符串、数字、元组、List
6、字典的优点,同哈希表:查询快、存储快(冲突少的情况下)、删除快
7、【增】有多个元素的字典语法,key与value用冒号隔开,每个元素(key-value)之间用逗号隔开
>>> tempB = {5:"hello", "two":999, 100:23}
>>> tempB
{100: 23, 5: 'hello', 'two': 999}
你看我例子中,key的类型分别使用了整型、字符串
value的类型分别有整型、字符串
8、字典的嵌套,key用字典可以吗?答案是不可以
>>> tempA = {}
>>> tempB = {5:"hello", "two":999, 100:23}
>>> tempB
{100: 23, 5: 'hello', 'two': 999}
>>> tempSuper = {tempB:"Hello", tempA:"World"}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
解答:一个对象能不能作为字典的key,就取决于其有没有__hash__方法。所以所有python自带类型中,除了list、dict、set和内部至少带有上述三种类型之一的tuple之外,其余的对象都能当key
9、key必须是独一无二的,即字典中的key不可以有重复的,如果重复了,会把原来的key-value中的value覆盖掉
10、value可以是重复的,而且可以是任意类型的对象,上面已经说过了,这里就是想再说一次,比如value是List、元组、字典,都没有问题
>>> tempA = {}
>>> tempB = {5:"hello", "two":999, 100:23}
>>> tempSuper = {"first":tempA,"second":tempB}
>>> tempSuper
{'second': {100: 23, 5: 'hello', 'two': 999}, 'first': {}}
11、【查】如何访问字典中的value啊,语法自然是 字典[key],如果key不存在,肯定是要报错的
>>> tempSuper["second"]
{100: 23, 5: 'hello', 'two': 999}
12、【查】返回字典中所有的key,作为一个List返回
#python就厉害在,如果你不加print,人家也会给你打印出来,牛x
>>> print(tempSuper.keys())
['second', 'first']
>>> tempSuper.keys()
['second', 'first']
13、【查】使用字典中的函数get(传入key), 字典.get(key),如果key不存在,返回None
>>> tempSuper.get("first")
14、【查】还有一个重载的使用字典中的函数get(传入key,传入找不到key的容错方案),比如
>>> tempSuper.get("third","not found")
'not found'
15、【查】判断key是否在一个字典中
>>> "first" in tempSuper
True
16、【查】刚才我还想说,字典怎么遍历呢,怎么报错了?后面看答案
>>> tempSuper
{'second': {100: 23, 5: 'hello', 'two': 999}, 'first': {}}
>>> for k,v in tempSuper:
... print k,v
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
17、【查】返回字典中所有的value,作为一个List返回,别懵逼,我两个value:一个是两个元素(Entry)的字典、一个是空字典
>>> tempSuper.values()
[{100: 23, 5: 'hello', 'two': 999}, {}]
18、【查】这次遍历数组靠谱,调用了字典的items()方法,艹
>>> tempSuper
{'second': {100: 23, 5: 'hello', 'two': 999}, 'first': {}}
>>> for k,v in tempSuper.items():
... print k,v
...
second {100: 23, 5: 'hello', 'two': 999}
first {}
19、遍历字典中的key
>>> for k in tempSuper:
... print(k)
...
second
first
20、修改字典,如果key存在,就修改,如果key不存在,就是新增
>>> tempSuper
{'second': {100: 23, 5: 'hello', 'two': 999}, 'first': {}}
>>> tempSuper['second'] = 'baby'
>>> tempSuper['third'] = 'the three'
>>> tempSuper
{'second': 'baby', 'third': 'the three', 'first': {}}
21、字典是无序的,所以你插入元素的位置都是随机的
22、删除字典元素
>>> tempSuper
{'second': 'baby', 'third': 'the three', 'first': {}}
>>> tempSuper.pop("second")
'baby'
>>> tempSuper
{'third': 'the three', 'first': {}}
23、随时删除字典中的一个元素,popitem()
>>> tempSuper.popitem()
('third', 'the three')
>>> tempSuper
{'first': {}}
24、使用del语句嘛
>>> del tempSuper['first']
>>> tempSuper
{}
25、 清空字典,直接把字典中所有元素都干掉了
>>> tempSuper.clear()
26、其他字典常见操作(纯转载)
dict.items() 输出一个list格式(非真正意义上的list)
list(dict.items()) 把字典的key 和 value 转成一个多维list
len(dict):计算字典元素个数,即键的总数。
str(dict):输出字典可打印的字符串。
type(variable):返回输入的变量类型,如果变量是字典就返回字典类型
27、利用not in判断key是否不在字典中
key not in dict