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

标签 python dictionary key

任务是获取与 Actor 字典相关的所有电影。

actor_dict = {'actor_A': [m1, m2, m3, m4], 'actor_B': [m5,m6,m7,m8]}

如何从整个字典中挑选出值列表?

最佳答案

一种可能性是:

movies_list = []
for v in actor_dict.itervalues():
    movies_list.extend(v)

如果您想要独特的电影(如果一部电影出现在多个 Actor 的列表中):

movies_set = []
for v in actor_dict.itervalues():
    movies_set.update(v)
movies_list = list(movies_set)

或者:

movies_list = list(reduce(set.union, map(set, d.itervalues())))

或者(感谢@DrTyrsa):

movies_list = list(set.union(*map(set, d.itervalues())))

关于Python:获取与字典中单个键关联的值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8337675/

相关文章:

ios - 使用 "po"从控制台日志中的服务器响应中获取数据

dictionary - 获取 map 键之间的所有范围

javascript - 从 JSON 中获取 KEY

python - 为什么在该函数的 return 语句中实现海象运算符时不起作用?

Python 裁剪数据库返回的变量

python - 使用map和setattr函数动态设置对象属性

MySQL 错误 1215 外键问题

python - 在 SWIG 中获取从 C++ 到 Python 的 char* 输出

python - Sorted() 排序不正确

python - 从 JSON 中提取数据并使用 python 进行迭代