Python字典—;通过输入值来查找键

Python字典—;通过输入值来查找键,python,dictionary,Python,Dictionary,我有一些代码,但我不知道如何使用值在字典中查找键。这里有我的代码和我想做的事情: NAMES = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank', 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry'] AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19] def combine(list1,list2): dic

我有一些代码,但我不知道如何使用值在字典中查找键。这里有我的代码和我想做的事情:

NAMES = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank',
         'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']

AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]


def combine(list1,list2):
    dictionary = dict(zip(list1,list2))
    return dictionary

def people(age):
    if leeftijd in dictionary:
        return ['Bob']


print combine(NAMES, AGES)

print people(21) == ['Bob'] 
print people(22) == ['Kelly']
print people(23) == []
在这段代码中,我首先使用combine函数将列表中的姓名和年龄放入字典,但现在我想创建第二个名为
people
的函数,该函数将输出输入年龄的所有姓名

例如:如果我在该函数中输入21,它应该输出Bob,因为Bob是唯一一个21岁的人


我只是不知道怎么做,因为年龄不是我字典里的关键,我也不能用整数作为关键。

事实上,据我所知,没有办法

只需反复阅读你的口述:

people_array = combine(NAMES, AGES)
def people(age):
    for person in people_array:
        if people_array[person] == age:
            print(person)

可以这样做

for name, age in mydict.items():
    if age == search_age:
        print name
尝试此功能(
为值键入):


一个很好的方法是:

def people(age):
    ages = [i for i,j in enumerate(combine.values()) if j == age]
    return [combine.keys()[i] for i in ages]
试试这个:

def people(age):
    return map(lambda x: x[0], filter(lambda x: x[1]==age, dictionary.items()))

正如我在a,AFAIK中提到的,如果你不只是通过字典进行线性搜索,你必须维护一个反向映射(字典),它需要为每个值维护一个键列表(因为数据中有2个18岁的、5个19岁的和3个20岁的)

此代码包括一个(通用)函数
reverse\u dictionary()
,它接受给定的字典并从中创建反向映射。函数中的
name
age
变量名是我为这个问题编写的一个赠品,但是将它们更改为
key
value
,代码可以在值可散列的任何字典上工作

NAMES = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank',
         'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']

AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]

def reverse_dictionary(fwd):
    rev = {}
    for name, age in fwd.items():
        if age not in rev:
            rev[age] = []
        rev[age].append(name)
    return rev

def combine(list1,list2):
    dictionary = dict(zip(list1,list2))
    return dictionary

def people(age, dictionary):
    if age not in dictionary:
        return []
    return dictionary[age]

name_age = combine(NAMES, AGES)
print name_age

age_name = reverse_dictionary(name_age)

for age in range(17, 24):
    print age, people(age, age_name)
样本输出:

{'Ed': 19, 'Dan': 18, 'Gary': 20, 'Alice': 20, 'Kelly': 22, 'Larry': 19, 'Jack': 19, 'Frank': 20, 'Cathy': 18, 'Bob': 21, 'Irene': 19, 'Helen': 19}
17 []
18 ['Dan', 'Cathy']
19 ['Ed', 'Larry', 'Jack', 'Irene', 'Helen']
20 ['Gary', 'Alice', 'Frank']
21 ['Bob']
22 ['Kelly']
23 []

请注意,这不会使两个词典保持同步;如果在
name\u age
字典中添加新的名称和年龄,还需要将年龄和名称添加到反向字典中。另一方面,如果加载一次姓名/年龄映射,然后它保持稳定,并且需要根据年龄进行多次查找,那么,反向字典可能比每次搜索都通过名称/年龄字典进行迭代的解决方案效率更高。

您完全可以使用整数作为字典键-它是可散列且不可变的。您将如何处理两个21岁的人?另外,请参见
people()
中的
leeftijd
,这是
年龄的打字错误(漏译)
?(Google translate会将“leeftijd”转换为“age”。)好吧,如果你不只是在字典中进行线性搜索,你必须维护一个反向映射(字典),它需要为每个值维护一个键列表(因为数据中有2个18岁的、4个19岁的和2个20岁的)。如果您发现自己想要这样做,则可能使用了错误的数据结构。您应该反转字典,或者使用双向字典。例如:通过使用combine,我指的是combine的结果变量(名称、年龄)
{'Ed': 19, 'Dan': 18, 'Gary': 20, 'Alice': 20, 'Kelly': 22, 'Larry': 19, 'Jack': 19, 'Frank': 20, 'Cathy': 18, 'Bob': 21, 'Irene': 19, 'Helen': 19}
17 []
18 ['Dan', 'Cathy']
19 ['Ed', 'Larry', 'Jack', 'Irene', 'Helen']
20 ['Gary', 'Alice', 'Frank']
21 ['Bob']
22 ['Kelly']
23 []