我做了一个函数,它将在字典中查找年龄并显示匹配的名字:
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。我知道这是不正确的,但我不知道如何让它向后搜索。
我意识到已经有很长一段时间了,最初的提问者可能不再需要答案,但如果您实际上可以控制这段代码,那么这些答案都不是好的答案。您只是使用了错误的数据结构。这是双向字典用例的完美说明:
>>> from collections import defaultdict, UserDict
>>> class TwoWayDict(UserDict):
... def __init__(self, *args, **kwargs):
... super().__init__(*args, **kwargs)
... self.val_to_keys = defaultdict(list)
... def __setitem__(self, key, value):
... super().__setitem__(key, value)
... self.val_to_keys[value].append(key)
... def get_keys_for_val(self, value):
... return self.val_to_keys[value]
...
>>> d = TwoWayDict()
>>> d['a'] = 1
>>> d['b'] = 1
>>> d.get_keys_for_val(1)
['a', 'b']
为插入增加了极小的开销,但您保持了恒定的查找时间,除了现在是双向查找。不需要在每次需要时从头构造反向映射。只要在你需要的时候存储它并访问它。
此外,这些答案中有许多甚至是不正确的,因为很明显,许多人可能具有相同的年龄,但他们只返回第一个匹配的键,而不是所有的键。
以下是我对这个问题的看法。:)
我刚刚开始学习Python,所以我称之为:
“初学者可以理解的”解决方案。
#Code without comments.
list1 = {'george':16,'amber':19, 'Garry':19}
search_age = raw_input("Provide age: ")
print
search_age = int(search_age)
listByAge = {}
for name, age in list1.items():
if age == search_age:
age = str(age)
results = name + " " +age
print results
age2 = int(age)
listByAge[name] = listByAge.get(name,0)+age2
print
print listByAge
.
#Code with comments.
#I've added another name with the same age to the list.
list1 = {'george':16,'amber':19, 'Garry':19}
#Original code.
search_age = raw_input("Provide age: ")
print
#Because raw_input gives a string, we need to convert it to int,
#so we can search the dictionary list with it.
search_age = int(search_age)
#Here we define another empty dictionary, to store the results in a more
#permanent way.
listByAge = {}
#We use double variable iteration, so we get both the name and age
#on each run of the loop.
for name, age in list1.items():
#Here we check if the User Defined age = the age parameter
#for this run of the loop.
if age == search_age:
#Here we convert Age back to string, because we will concatenate it
#with the person's name.
age = str(age)
#Here we concatenate.
results = name + " " +age
#If you want just the names and ages displayed you can delete
#the code after "print results". If you want them stored, don't...
print results
#Here we create a second variable that uses the value of
#the age for the current person in the list.
#For example if "Anna" is "10", age2 = 10,
#integer value which we can use in addition.
age2 = int(age)
#Here we use the method that checks or creates values in dictionaries.
#We create a new entry for each name that matches the User Defined Age
#with default value of 0, and then we add the value from age2.
listByAge[name] = listByAge.get(name,0)+age2
#Here we print the new dictionary with the users with User Defined Age.
print
print listByAge
.
#Results
Running: *\test.py (Thu Jun 06 05:10:02 2013)
Provide age: 19
amber 19
Garry 19
{'amber': 19, 'Garry': 19}
Execution Successful!
下面是一个在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,因为前者是预定义的类型。