Python 为循环添加了额外的字典键
我正在尝试编写一个python函数,该函数将以电影标题为键的dict和该电影中的演员列表作为值,并返回一个新dict,其中演员作为键,他们在其中出现的电影作为值。这段代码很接近,但每次运行都会返回不同的结果 以下是原始函数调用:Python 为循环添加了额外的字典键,python,dictionary,for-loop,Python,Dictionary,For Loop,我正在尝试编写一个python函数,该函数将以电影标题为键的dict和该电影中的演员列表作为值,并返回一个新dict,其中演员作为键,他们在其中出现的电影作为值。这段代码很接近,但每次运行都会返回不同的结果 以下是原始函数调用: movies = {"How to Be Single": ["Alison Brie", "Dakota Johnson", "Rebel Wilson"], "The
movies = {"How to Be Single": ["Alison Brie", "Dakota Johnson",
"Rebel Wilson"],
"The Lego Movie": ["Will Arnett", "Elizabeth Banks",
"Alison Brie", "Will Ferrell"]}
print(stars,movies)
代码如下:
def stars(movies):
# create a new dictionary
films_dict= {}
performances_films = []
# for each movie actor, add as key to new_dict
for movie, actors in movies.items():
for actor in actors:
if not actor in films_dict.keys():
#print(actor)
performances_films = []
performances_films.append(movie)
films_dict[actor] = performances_films
else:
performances_films.append(movie)
films_dict[actor] = performances_films
return(films_dict)
以下是输出:
RUN 1
{'Rebel Wilson': ['How to Be Single'], 'Will Ferrell': ['The Lego
Movie'],
'Will Arnett': ['The Lego Movie'], 'Elizabeth Banks': ['The Lego Movie',
'The Lego Movie'], 'Alison Brie': ['The Lego Movie', 'The Lego Movie'],
'Dakota Johnson': ['How to Be Single']}
RUN 2
{'Will Arnett': ['The Lego Movie'], 'Dakota Johnson': ['How to Be Single'], 'Elizabeth Banks': ['The Lego Movie', 'The Lego Movie'], 'Rebel Wilson': ['How to Be Single'], 'Will Ferrell': ['The Lego Movie'], 'Alison Brie': ['The Lego Movie', 'The Lego Movie']}
我认为它要么在每次运行时以不同的顺序遍历dict项,要么在一次运行到下一次运行时保存数据。不管怎样,我似乎都无法解决这个问题。我真的不明白你为什么要把
性能\u电影
列表放在周围。您只需使用先前在dict
本身中创建的代码即可
以下是我认为有效的方法:
def stars(movies):
# create a new dictionary
films_dict= {}
# for each movie actor, add as key to new_dict
for movie, actors in movies.items():
for actor in actors:
if not actor in films_dict.keys():
performances_films = []
performances_films.append(movie)
films_dict[actor] = performances_films
else:
films_dict[actor].append(movie)
return(films_dict)
印刷品:
{'Alison Brie': ['How to Be Single', 'The Lego Movie'], 'Dakota Johnson': ['How to Be Single'], 'Rebel Wilson': ['How to Be Single'], 'Will Arnett': ['The Lego Movie'], 'Elizabeth Banks': ['The Lego Movie'], 'Will Ferrell': ['The Lego Movie']}
代码中的错误是,
列表
实例性能从一个循环迭代持续到下一个循环迭代。这就是为什么(比如)艾莉森·布里(Alison Brie)将乐高电影列了两次——本来应该为另一名演员添加,但之前为艾莉森·布里(Alison Brie)创建的列表
实例仍然占据了表演电影
的名称位置。事实上,看起来多个参与者可能共享同一个列表
实例
如果在访问字典时使用“默认值”概念,可以大大简化代码。这可以通过使用类来完成:
或者我更喜欢的方法是使用普通的dict及其方法:
无论哪种方式,您都可以统一对待每一部新电影,而无需明确测试演员先前的
从影记录成员资格
Python字典是无序的,这就是为什么每次运行的输出都会发生变化。如果您想保持词典中项目的顺序一致,您可能需要使用
{'Alison Brie': ['How to Be Single', 'The Lego Movie'], 'Dakota Johnson': ['How to Be Single'], 'Rebel Wilson': ['How to Be Single'], 'Will Arnett': ['The Lego Movie'], 'Elizabeth Banks': ['The Lego Movie'], 'Will Ferrell': ['The Lego Movie']}
import collections
filmography = collections.defaultdict(list)
for movie, actors in movies.items():
for actor in actors:
filmography[actor].append(movie)
filmography = {}
for movie, actors in movies.items():
for actor in actors:
filmography.setdefault(actor, []).append(movie)