小编Asd*_*dfg的帖子

在python中反转字典

我有这本字典

{'jackie chan': ('rush hour', 'rush hour 2'), 
 'crish tucker': ('rush hour', 'rush    hour 2')}
Run Code Online (Sandbox Code Playgroud)

我想要逆字典

{'rush hour': ('jackie chan', 'crish tucker'), 
 'rush hour 2': ('jackie chan', 'crish tucker')}
Run Code Online (Sandbox Code Playgroud)

我已经将函数反转但它看起来不像第二个字典

def invert_actor_dict(actor_dict):
    movie_dict = {}
    for key,value in actor_dict.iteritems():

        for actor in value:
            if actor in movie_dict:
                movie_dict[actor].append(key)
            else:
                movie_dict[actor] = (key)
    return movie_dict
Run Code Online (Sandbox Code Playgroud)

python dictionary

5
推荐指数
2
解决办法
869
查看次数

标签 统计

dictionary ×1

python ×1