查找与给定值共享密钥的所有值

时间:2014-11-10 00:49:52

标签: python dictionary

我有两个字典,一个{演员:电影}和另一个{电影:演员},我试图创建一个程序,告诉我输入演员的所有合作者。

例如:

dictionary1看起来像这样:

{'Daniel Radcliffe':['Harry Potter'], 'Emma Thompson':['Harry Potter','Sense and Sensibility'], 'Alan Rickman':['Harry Potter', 'Sense and Sensibility']}

dictionary2看起来像这样:

{'Harry Potter': ['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman'], 'Sense and Sensibility': ['Emma Thompson', 'Alan Rickman', 'Hugh Grant']}

如果我输入Alan Rickman,输出应为:

['Daniel Radcliffe','Emma Thompson','Hugh Grant']

这是我目前的代码:

def coactors(actor):
    dictionary1 = makeDictionaryFromFile()
    dictionary2 = makeReverseDictionary()
    films=dictionary1[actor]
    for n in films:
        if n in dictionary2.keys():
           return list(set(dictionary2[n]))
        else:
            return "There was an error, try again"

,输出为:

['Daniel Radcliffe']
它似乎只是阅读列表中的第一部电影,而不是遍历其余部分。我做错了什么?

5 个答案:

答案 0 :(得分:0)

您应该遍历所有电影并附加结果而不是立即返回。 使用您当前的代码,只会返回电影中第一部电影中的合作者。

def coactors(actor):
dictionary1 = makeDictionaryFromFile()
dictionary2 = makeReverseDictionary()
films=dictionary1[actor]
coacts = []
for n in films:
    if n in dictionary2.keys():
        coacts += list(set(dictionary2[n]))
    else:
        return "There was an error, try again"
return coacts.remove(actor)

答案 1 :(得分:0)

for n in films:循环替换为:

results = set(flat for flat in dictionary2.get(film, []) for film in films)
results.remove(actor)
return results

答案 2 :(得分:0)

尝试浏览d2.values()它会为您提供一个列表中存储的所有密钥的值列表。在您的情况下,您将获得一个列表列表。您可以浏览项目(原来是键的值),每当您在列表中找到该演员时,您存储此列表,如果您找不到演员,则不需要添加此项名单。您将在最后得到一个列表列表。您可以使用Reduce,它可以帮助您将列表列表转换为列表,set可以帮助您删除重复项,然后从此列表中删除actor的名称以获取共同参与者的名称。

代码如下:

d1={'Daniel Radcliffe':['Harry Potter'], 'Emma Thompson':['Harry Potter','Sense and Sensibility'], 'Alan Rickman':['Harry Potter', 'Sense and Sensibility']}
d2={'Harry Potter': ['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman'], 'Sense and Sensibility': ['Emma Thompson', 'Alan Rickman', 'Hugh Grant']}

def coactors(actor):
    d=[list(set(x)) for x in d2.values() if actor in x]
    return list(set(reduce(lambda x,y:x+y,d))-{actor}) 

print coactors('Alan Rickman') #Output: ['Hugh Grant', 'Daniel Radcliffe', 'Emma Thompson']

答案 3 :(得分:0)

尝试这样:

def coactors(actor):
    co_actors = []
    dictionary1 = makeDictionaryFromFile()
    dictionary2 = makeReverseDictionary()
    films=dictionary1[actor]
    for n in films:
        if n in dictionary2.keys():
            co_actors +=dictionary2[n]
    if len(co_actors) == 0:
        return "error"
    else:
        co_actors.remove(actor)
        return list(set(co_actors))

答案 4 :(得分:0)

 l1 = {'Daniel Radcliffe':['Harry Potter'], 'Emma Thompson':['Harry Potter','Sense and Sensibility'], 'Alan Rickman':['Harry Potter', 'Sense and Sensibility']}
 l2 = {'Harry Potter': ['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman'], 'Sense and Sensibility': ['Emma Thompson', 'Alan Rickman', 'Hugh Grant']}

 if __name__ == "__main__":
     name = 'Alan Rickman'
     films = l1.get(name, None)
     if films:
         for film, names in l2.items():
             if film in films:
                 print names

输出结果为:

['Daniel Radcliffe', 'Emma Thompson', 'Alan Rickman']
['Emma Thompson', 'Alan Rickman', 'Hugh Grant']