python字典如何用value取对应的key值

1、用value取key值
a = {"dd":34,"tt":44,"yy":88,"rr":"uu"}
h = list(a.keys())[list(a.values()).index(44)]
print(h)
结果:tt
注:引用一段Python3文档里面的原话:

If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.
 

翻译:在你迭代的过程中如果没有发生对字典的修改,那么.keys() and .values 这两个函数返回的 dict-view对象总是保持对应关系。

2、取出全部keys值

a = {"dd":34,"tt":44,"yy":88,"rr":"uu"}

for i in a.keys() :
    print(i,end=",")

结果:dd,tt,yy,rr,

3、取出全部value值

a = {"dd":34,"tt":44,"yy":88,"rr":"uu"}

for i in a.values() :
    print(i,end=",")
结果:34,44,88,uu,

4、取出全部keys和value值

a = {"dd":34,"tt":44,"yy":88,"rr":"uu"}

for i in a.items() :
    print(i,end=",")
结果:('dd', 34),('tt', 44),('yy', 88),('rr', 'uu'),

你可能感兴趣的:(python)