[英]Python: Getting a list of values associated with a single key in a dictionary
The task is to take all the movies associated with a dictionary of actors. 任务是拍摄与演员字典相关的所有电影。
actor_dict = {'actor_A': [m1, m2, m3, m4], 'actor_B': [m5,m6,m7,m8]}
How can I single out the list of values from the entire dictionary? 如何从整个字典中选出值列表?
One possibility would be: 一种可能性是:
movies_list = []
for v in actor_dict.itervalues():
movies_list.extend(v)
If you want unique movies (if one movie appears in more actors' lists): 如果要制作独特的电影(如果一部电影出现在更多演员的列表中):
movies_set = []
for v in actor_dict.itervalues():
movies_set.update(v)
movies_list = list(movies_set)
or: 要么:
movies_list = list(reduce(set.union, map(set, d.itervalues())))
or (thanks to @DrTyrsa): 或(由于@DrTyrsa):
movies_list = list(set.union(*map(set, d.itervalues())))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.