我做了一个函数,它将在字典中查找年龄并显示匹配的名字:
dictionary = {'george' : 16, 'amber' : 19}
search_age = raw_input("Provide age")
for age in dictionary.values():
if age == search_age:
name = dictionary[age]
print name
我知道如何比较和查找年龄,只是不知道如何显示这个人的名字。此外,由于第5行,我得到了一个KeyError。我知道这是不正确的,但我不知道如何让它向后搜索。
我发现这个答案很有效,但对我来说不太容易理解。
为了使它更清楚,您可以反转字典的键和值。这是使键值和值键,如这里所示。
mydict = {'george':16,'amber':19}
res = dict((v,k) for k,v in mydict.iteritems())
print(res[16]) # Prints george
或者Python 3,(谢谢@kkgarg)
mydict = {'george':16,'amber':19}
res = dict((v,k) for k,v in mydict.items())
print(res[16]) # Prints george
Also
print(res.get(16)) # Prints george
本质上和另一个答案是一样的。
我试着阅读尽可能多的答案,以防止给出重复的答案。然而,如果你正在处理一个包含在列表中的值的字典,并且如果你想获得具有特定元素的键,你可以这样做:
d = {'Adams': [18, 29, 30],
'Allen': [9, 27],
'Anderson': [24, 26],
'Bailey': [7, 30],
'Baker': [31, 7, 10, 19],
'Barnes': [22, 31, 10, 21],
'Bell': [2, 24, 17, 26]}
现在让我们找到值为24的名称。
for key in d.keys():
if 24 in d[key]:
print(key)
这也适用于多个值。
下面是一个在python2和python3中都适用的解决方案:
dict((v, k) for k, v in list.items())[search_age]
直到[search_age]的部分构造反向字典(其中值是键,反之亦然)。
你可以创建一个helper方法来缓存这个反向字典,就像这样:
def find_name(age, _rev_lookup=dict((v, k) for k, v in ages_by_name.items())):
return _rev_lookup[age]
或者更一般的是一个工厂,它会为你的一个或多个列表创建一个按年龄查找的方法
def create_name_finder(ages_by_name):
names_by_age = dict((v, k) for k, v in ages_by_name.items())
def find_name(age):
return names_by_age[age]
所以你可以这样做:
find_teen_by_age = create_name_finder({'george':16,'amber':19})
...
find_teen_by_age(search_age)
注意,我将list重命名为ages_by_name,因为前者是预定义的类型。
这是一个真正的“可逆字典”,基于Adam Acosta的解决方案,但强制val-to-key调用是唯一的,容易从值返回键:
from collections import UserDict
class ReversibleDict(UserDict):
def __init__(self, enforce_unique=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.val_to_keys = {}
self.check_val = self.check_unique if enforce_unique else lambda x: x
def __setitem__(self, key, value):
self.check_val(value)
super().__setitem__(key, value)
self.val_to_keys[value] = key
def __call__(self, value):
return self.val_to_keys[value]
def check_unique(self, value):
assert value not in self.val_to_keys, f"Non unique value '{value}'"
return value
如果你想强制字典值的唯一性,确保set enforce_unique=True。从值中获取键只需做rev_dict(value),从键中调用值只需像往常一样做dict['key'],这里是一个用法示例:
rev_dict = ReversibleDict(enforce_unique=True)
rev_dict["a"] = 1
rev_dict["b"] = 2
rev_dict["c"] = 3
print("full dictinoary is: ", rev_dict)
print("value for key 'b' is: ", rev_dict["b"])
print("key for value '2' is: ", rev_dict(2))
print("tring to set another key with the same value results in error: ")
rev_dict["d"] = 1