The python dictionary queries

Value read dictionary specific key corresponding to
the first method, direct read

D={"键1":"值1","key2":"value2"}
v=D["key2"]		# 没有这个键会报错
print(v)

Here Insert Picture Description
The second method, using the dictionary get () function

D={"键1":"值1","key2":"value2"}
a=D.get("键12")  # 没有这个键时或返回: None
print(a)

Here Insert Picture Description
According to the dictionary query key value

D={"键1":"值1","key2":"value2"}
k="value2"
for i,j in D.items():  # items()读取所有的键值,i 是键 ,j 是值
    if j == k :
        print(i)

Here Insert Picture Description
All dictionaries built-in functions
https://blog.csdn.net/GrofChen/article/details/91371816
all key-value pairs read
https://blog.csdn.net/GrofChen/article/details/91488972

Guess you like

Origin blog.csdn.net/GrofChen/article/details/91489121