python - 如何在 Python 中拆分字典数组?

标签 python dictionary

[{50: 2, 75: 3, 99: 4}, {50: 5, 75: 6, 99: 7}, {50: 8, 75: 9, 99:10}] 

以下是 Python 中的字典数组。我想在 Python 中将数组转换为以下三个数组:

[2, 5, 8] # corresponding to 50.
[3, 6, 9] # corresponding to 75.
[4, 7, 9] # corresponding to 99. 

最佳答案

一种方法可能是使用 defaultdict,这样它就是一个字典,其中 list 的值对应于键:

from collections import defaultdict

my_list = [{50: 2, 75: 3, 99: 4}, {50: 5, 75: 6, 99: 7}, {50: 8, 75: 9, 99:10}] 
my_dict_list = defaultdict(list)

for value in my_list:
    for k,v in value.items():
        my_dict_list[k].append(v)

每个值的列表可以通过如下键访问:

print(my_dict_list[50])

关于python - 如何在 Python 中拆分字典数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618355/

相关文章:

python - Keras - 从目录结构中获取标签作为字符串

通过函数的 Python 对象

python - DynamoDB : Create Table with keytype for nested json using Boto3

Python popen shell 命令等待子进程完成

python - 如何删除嵌套容器中的重复条目

dictionary - 迭代并从 map 中获取值

dictionary - 如何在 map[int] 类型上实现接口(interface)

python - 创建绘图的 Pytest 测试函数

python - 在 python 中遍历字典中的项目

python - 按字典中元组的元素组织?