(1) Getting to know the dictionary for the first time
In the list, we find the corresponding object through the " subscript number " . Find the corresponding " value object " through the " key object " in the dictionary .
-
A " key " is any immutable data such as: integers, floats, strings, tuples.
-
However, variable objects such as lists, dictionaries, and collections cannot be used as " keys " .
-
" Key " is not repeatable.
-
" Value " can be arbitrary data and can be repeated.
(2) Creation of dictionary
Create a dictionary object through {} , dict()
a ={'name':'baibi','age':18,'job':'programmer'}
b =dict(name='baibi',age=18,job='programmer')
a = dict([("name","baibi"),("age",18)])
c = {} #空的字典对象
d = dict() #空的字典对象
Create a dictionary object through zip()
Specific code:
k = ['name','age','job']
v = ['baibi',18,'teacher']
d = dict(zip(k,v))
print(d) #{'name': 'baibi', 'age': 18,'job': 'techer'}
Create a dictionary with empty values by fromkeys
f = dict.fromkeys(['name','age','job'])
print(f) #结果:{'name': None, 'age':None, 'job': None}
(3) Access to dictionary elements
Get " value " by [ key ] .
a ={'name':'baibi','age':18,'job':'programmer'}
b = a['name']
print(b)
The " value " is obtained by the get() method .
a ={'name':'baibi','age':18,'job':'programmer'}
b = a.get('name')
c = a.get('gender','不存在')
print(b)
print(c)
List all key-value pairs
a ={'name':'baibi','age':18,'job':'programmer'}
b = a.items()
print(b) #dict_items([('name', 'baibi'),('age', 18), ('job', 'programmer')])
List all keys, list all values
a ={'name':'baibi','age':18,'job':'programmer'}
k = a.keys()
v = a.values()
print(k) #dict_keys(['name', 'age','job'])
print(v) #dict_values(['baibi', 18,'programmer'])
len() the number of key-value pairs
a ={'name':'baibi','age':18,'job':'programmer'}
num = len(a)
print(num) #3
Check if a " key " is in the dictionary
a ={'name':'baibi','age':18,'job':'programmer'}
print("name" in a) #True
(4) Adding, modifying, and deleting dictionary elements
Add " key-value pairs " to the dictionary .
If the " key " already exists, the old key-value pair will be overwritten; if the " key " does not exist, a new " key-value pair " will be added.
a ={'name':'baibi','age':18,'job':'programmer'}
a['address']='毛列街道1号院'
a['age']=16
print(a)
#{'name': 'baibi', 'age': 16, 'job':'programmer', 'address': '毛列街道1号院'}
use update()
Add all key-value pairs in the new dictionary to the old dictionary object. If the key is duplicated, it will be directly overwritten
a ={'name':'gaoqi','age':18,'job':'programmer'}
b ={'name':'gaoxixi','money':1000,'gender':'男的'}
a.update(b)
print(a)
#{'name': 'gaoxixi', 'age': 18, 'job':'programmer', 'money': 1000, 'gender': '男的'}
Deletion of elements in a dictionary
You can use the del() method; or clear() to delete all key-value pairs; pop() deletes the specified key-value pair and returns the corresponding " value object ".
a ={'name':'baibi','age':18,'job':'programmer'}
del(a['name'])
print(a) #{'age': 18, 'job':'programmer'}
age = a.pop('age')
print(age) #18
drink() :
a ={'name':'gaoqi','age':18,'job':'programmer'}
r1 = a.popitem()
r2 = a.popitem()
r3 = a.popitem()
print(a) #{}
(5) Sequence unpacking
s = {'name':'baibi','age':18,'job':'teacher'}
name,age,job=s #默认对键进行操作
print(name) #name
name,age,job=s.items() #对键值对进行操作
print(name) #('name', 'baibi')
name,age,job=s.values() #对值进行操作
print(name) #baibi
(6) Table data is stored and accessed using dictionaries and lists
Name | age | salary | City |
Zhao Gao | 18 | 30000 | Beijing |
Wang An | 19 | 20000 | Shanghai |
r1 = {"name":"赵高","age":18,"salary":30000,"city":"北京"}
r2 = {"name":"王安","age":19,"salary":20000,"city":"上海"}
tb = [r1,r2]
#获得第二行的人的薪资
print(tb[1].get("salary"))
#打印表中所有的的薪资
for i in range(len(tb)): # i -->0,1,2
print(tb[i].get("salary"))
#打印表的所有数据
for i in range(len(tb)):
print(tb[i].get("name"),tb[i].get("age"),tb[i].get("salary"),tb[i].get("city"))
(7) The core underlying principles of the dictionary
a = {}a [ "name" ]= "libai"
print(bin(hash("name")))#-0b100001100010000110000100011111010111110010110100010111111111101