---Restore content begins---
The dictionary is composed of each set of key-value pairs. Each key-value pair is relational data, and the query speed is very fast. Search by binary method
dic = {'key':'value','name':'yy','hobby':'heheh','list':[2,3,'ee'],2:True
....}
Another way of categorizing data types
Mutable data types (non-hashable classes): list, dict, set
Immutable data type (hashable class): str int bool tuple
Container type (carries various data types): list dic tuple
Addition, deletion, modification and lookup of dictionary
The keys of a dictionary are unique, and there cannot be two identical keys in a dictionary
The order of the dictionary: unordered before 3.5, ordered after 3.6 (insert in a certain order when creating a dictionary, it looks ordered)
Addition, deletion and modification of dictionaries. check
1. Hash table Each dictionary has a hash table to locate each key-value pair through the hash table for easy lookup
dic = {'key':'value','name':'yy','hobby':'heheh','list':[2,3,'ee'],2:True}
print(hash('key'))
print(hash('value'))
#-2335181179344269333
#5043510051693866644
Each key-value pair has a hash number, and the hash value is constantly changing
dictionary addition
The first one: Overwrite if there is, add if not
dic = {'key':'value','name':'yy','hobby':'heheh','list':[2,3,'ee'],2:True}
dic['姓名'] = '张三'
dic['key'] = 'hehe'
print(dic)
#{'list': [2, 3, 'ee'], 2: True, 'name': 'yy', '姓名': ['张三'], 'key': 'value', 'hobby': 'heheh'}
#{'list': [2, 3, 'ee'], 'key': 'hehe', 2: True, 'name': 'yy', 'hobby': 'heheh'}
The second setdefault is added if there is no change
dic = {'key':'value','name':'yy','hobby':'heheh'}
dic.set.default('key','大炮')
dic.set.default('sex','femal')
print(dic)
# dic = {'key':'value','name':'yy','hobby':'heheh'}
# dic = {'key':'value','name':'yy','sex':'femal',hobby':'heheh'} 也是无序的增加进去
第三种 update
dic1.update(dic2)
将dic2中的键值对加入到dic1中,如果dic2中的键和dic1中的重复,则用dic2中的值覆盖dic1中的值 反之亦然
字典的删除
dic = {'key':'value','name':'yy','hobby':'heheh'}
第一种: pop 有返回值
print(dic.pop('key') 只能删除键 打印的结果会是这个键对应的值 如果删除值 会报错
# value
print(dic.pop('sex',None) 如果你要删除的键在这个字典中不存在,这种会返回你设定的值None 如果不设置的话 会报错
# None
第二种: clear
直接清空整个字典 留下空的字典
dic.clear()
print(dic)
#{}
第三种:del
dic = {'key':'value','name':'yy','hobby':'heheh'}
del dic
print(dic)
报错
del dic['key'] 只能通过键来删除值 如果删除的是值 会报错
print(dic)
#{'name':'yy','hobby':'heheh'}
第四种:popitem 这是个逗比功能 随机删除字典中的键值对 但是可以返回
print(dic.popitem())
字典的查
dic = {'key':'value','name':'yy','hobby':'heheh'}
第一种:通过键来查值
print(dic['key']}
# value
第二种:get 通过键来查找值
print(dic.get('key')
#value
print(dic.get('sex','你要查找的没有')) 如果要查找的键 不存在则默认返回None 如果自己定义了参数 则返回你定义的
#你要查找的没有
其他方法 C
dic.values() 把字典的键值对中的值 单独拉出来组成一个数据类型为dic_values的([])
print(dic.values()) dict_values(['heheh', 'value', 'yy'])
dic.keys() 把字典的键值对中的键 单独拉出来组成一个数据类型为dic_key的([])
print(dic.keys()) dict_keys(['name', 'hobby', 'key'])
dic.items() 把字典的键值拉出来组成一个数据类型为dic_items的([])
print(dic.items()) dict_items([('key', 'value'), ('name', 'yy'), ('hobby', 'heheh')])
以上三种方法得出来的数据类型只能循环 但是可以转换为列表的数据类型
分别赋值 这个很有意思
a,b = 2,3
print(a,b)
#a = 3
b = 2 一行代码把a b 的值互换
(a,b)= (2,3)
print(a,b) # 2,3
dic = {'key':'value','name':'yy','hobby':'heheh'}
for x,y in dic.items():
print(x,y)
#hobby heheh
name yy
key value
---恢复内容结束---