简体   繁体   English

Python:获取与字典中的单个键关联的值的列表

[英]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.

相关问题 Python:获取与字典中的键相关联的所有值,其中值可以是列表或单个项目 - Python: get all values associated with key in a dictionary, where the values may be a list or a single item Python程序来计算与字典中的键相关联的值 - Python program to count the values associated with key in a dictionary Python:将与键相关联的列表值存储在字典中 - Python: Storing a list value associated with a key in dictionary 如何检查在Dictionary,Python中与键相关联的值是否相等? - How to check if there are equal values associated to a key in Dictionary ,Python? 将多个值添加到python字典中的单个键 - Add multiple values to single key in python dictionary 删除 python 字典中单个键的多个值 - Remove multiples values of a single key in python dictionary 具有多个值的 Python 字典单键可能吗? - Python dictionary single key with multiple values possible? 搜索我的python字典中是否存在列表并附加到关联的键 - Searching if a list exists in my python dictionary and append to the associated key 将与字典中的键相关联的值列表与所有其他键相关联的值进行比较 - Comparing list of values associated with a key in a dictionary to the values associated all other keys 从使用列表的Python字典中获取关联值 - Getting associated values from Python dictionary that is using lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM