繁体   English   中英

使用键和值转换字典中的字典列表

[英]Converting a list of dictionaries in a dictionary using key and values

我有一个字典列表,有 156 个国家作为字典,每个国家有 9 个键,以分数作为值,如下列表:

[{'Overall rank': 1.0, 'Country': 'Finland', 'Score': 7.769, 'GDP per Capita': 1.34, 'Social Support': 1.587, 'Healthy Life Expectancy': 0.986, 'Freedom to Make Life Choices': 0.596, 'Generosity': 0.153, 'Perceptions of Corruption': 0.393}, {'Overall rank': 2.0, 'Country': 'Denmark', 'Score': 7.6, 'GDP per Capita': 1.383, 'Social Support': 1.573, 'Healthy Life Expectancy': 0.996, 'Freedom to Make Life Choices': 0.592, 'Generosity': 0.252, 'Perceptions of Corruption': 0.41}, {'Overall rank': 3.0, 'Country': 'Norway', 'Score': 7.554, 'GDP per Capita': 1.488, 'Social Support': 1.582, 'Healthy Life Expectancy': 1.028, 'Freedom to Make Life Choices': 0.603, 'Generosity': 0.271, 'Perceptions of Corruption': 0.341}, {'Overall rank': 4.0, 'Country': 'Iceland', 'Score': 7.494, 'GDP per Capita': 1.38, 'Social Support': 1.624, 'Healthy Life Expectancy': 1.026, 'Freedom to Make Life Choices': 0.591, 'Generosity': 0.354, 'Perceptions of Corruption': 0.118}, {'Overall rank': 5.0, 'Country': 'Netherlands', 'Score': 7.488, 'GDP per Capita': 1.396, 'Social Support': 1.522, 'Healthy Life Expectancy': 0.999, 'Freedom to Make Life Choices': 0.557, 'Generosity': 0.322, 'Perceptions of Corruption': 0.298}]

我想为每个键创建独立的字典,包括国家名称和变量的分数。

例如,人均 GDP 字典将是:

gdp_capita = {Finland: 1.34, Denmark: 1.383...}

我尝试使用这样的 for 循环:

for e in countries:
    gdp[e['GDP per Capita']] = gdp.setdefault(e, e['GDP per Capita'])

但它只能这样工作:

gdp = {}
for e in countries:
    gdp[e['GDP per Capita']] = gdp.setdefault(e['GDP per Capita'], 0) + 1

我错过了什么?

尝试这个:

d = [{"Overall rank": 1.0, "Country": "Finland", "Score": 7.769, "GDP per Capita": 1.34, "Social Support": 1.587, "Healthy Life Expectancy": 0.986, "Freedom to Make Life Choices": 0.596, "Generosity": 0.153, "Perceptions of Corruption": 0.393}, {"Overall rank": 2.0, "Country": "Denmark", "Score": 7.6, "GDP per Capita": 1.383, "Social Support": 1.573, "Healthy Life Expectancy": 0.996, "Freedom to Make Life Choices": 0.592, "Generosity": 0.252, "Perceptions of Corruption": 0.41}, {"Overall rank": 3.0, "Country": "Norway", "Score": 7.554, "GDP per Capita": 1.488, "Social Support": 1.582, "Healthy Life Expectancy": 1.028, "Freedom to Make Life Choices": 0.603, "Generosity": 0.271, "Perceptions of Corruption": 0.341}, {"Overall rank": 4.0, "Country": "Iceland", "Score": 7.494, "GDP per Capita": 1.38, "Social Support": 1.624, "Healthy Life Expectancy": 1.026, "Freedom to Make Life Choices": 0.591, "Generosity": 0.354, "Perceptions of Corruption": 0.118}, {"Overall rank": 5.0, "Country": "Netherlands", "Score": 7.488, "GDP per Capita": 1.396, "Social Support": 1.522, "Healthy Life Expectancy": 0.999, "Freedom to Make Life Choices": 0.557, "Generosity": 0.322, "Perceptions of Corruption": 0.298}]

gdp = {i["Country"]: i["GDP per Capita"] for i in d}
print(gdp)

输出: {'Finland': 1.34, 'Denmark': 1.383, 'Norway': 1.488, 'Iceland': 1.38, 'Netherlands': 1.396}

如果出于某种原因,您想使用国家/地区名称和每个键的值创建单独的字典,您可以这样做:

single_dicts = [
    {item["Country"]: value} for item in data 
    for value in item.values() if value != item["Country"]
]
print(single_dicts)

这输出:

[{'Finland': 1.0}, {'Finland': 7.769}, {'Finland': 1.34}, {'Finland': 1.587}, {'Finland': 0.986}, {'Finland': 0.596}, {'Finland': 0.153}, {'Finland': 0.393}, {'Denmark': 2.0}, {'Denmark': 7.6}, {'Denmark': 1.383}, {'Denmark': 1.573}, {'Denmark': 0.996}, {'Denmark': 0.592}, {'Denmark': 0.252}, {'Denmark': 0.41}, {'Norway': 3.0}, {'Norway': 7.554}, {'Norway': 1.488}, {'Norway': 1.582}, {'Norway': 1.028}, {'Norway': 0.603}, {'Norway': 0.271}, {'Norway': 0.341}, {'Iceland': 4.0}, {'Iceland': 7.494}, {'Iceland': 1.38}, {'Iceland': 1.624}, {'Iceland': 1.026}, {'Iceland': 0.591}, {'Iceland': 0.354}, {'Iceland': 0.118}, {'Netherlands': 5.0}, {'Netherlands': 7.488}, {'Netherlands': 1.396}, {'Netherlands': 1.522}, {'Netherlands': 0.999}, {'Netherlands': 0.557}, {'Netherlands': 0.322}, {'Netherlands': 0.298}]

如果你更喜欢这个神秘的列表理解是 ungolfed:

single_dicts = []
for item in data:
    for value in item.values():
        if value != item["Country"]:
            single_dicts.append({item["Country"]: value})
print(single_dicts)

这是你要找的吗? 它创建了一个字典,每个键都指向一个分数,每个值都是一个字典,以一个国家的名称作为键,并为该值提供分数:

list_of_dicts = [{'Overall rank': 1.0, 'Country': 'Finland', 'Score': 7.769, 'GDP per Capita': 1.34, 'Social Support': 1.587, 'Healthy Life Expectancy': 0.986, 'Freedom to Make Life Choices': 0.596, 'Generosity': 0.153, 'Perceptions of Corruption': 0.393}, {'Overall rank': 2.0, 'Country': 'Denmark', 'Score': 7.6, 'GDP per Capita': 1.383, 'Social Support': 1.573, 'Healthy Life Expectancy': 0.996, 'Freedom to Make Life Choices': 0.592, 'Generosity': 0.252, 'Perceptions of Corruption': 0.41}, {'Overall rank': 3.0, 'Country': 'Norway', 'Score': 7.554, 'GDP per Capita': 1.488, 'Social Support': 1.582, 'Healthy Life Expectancy': 1.028, 'Freedom to Make Life Choices': 0.603, 'Generosity': 0.271, 'Perceptions of Corruption': 0.341}, {'Overall rank': 4.0, 'Country': 'Iceland', 'Score': 7.494, 'GDP per Capita': 1.38, 'Social Support': 1.624, 'Healthy Life Expectancy': 1.026, 'Freedom to Make Life Choices': 0.591, 'Generosity': 0.354, 'Perceptions of Corruption': 0.118}, {'Overall rank': 5.0, 'Country': 'Netherlands', 'Score': 7.488, 'GDP per Capita': 1.396, 'Social Support': 1.522, 'Healthy Life Expectancy': 0.999, 'Freedom to Make Life Choices': 0.557, 'Generosity': 0.322, 'Perceptions of Corruption': 0.298}]

set_of_keys = set(
    key
    for country_dict in list_of_dicts
    for key in country_dict.keys()
)

dict_of_dicts = {
    key: {
        country_dict["Country"]: country_dict[key]
        for country_dict in list_of_dicts
    }
    for key in set_of_keys
}

for key, value in dict_of_dicts.items():
    print(key, ": ", value, sep="")

输出:

Overall rank: {'Finland': 1.0, 'Denmark': 2.0, 'Norway': 3.0, 'Iceland': 4.0, 'Netherlands': 5.0}
Score: {'Finland': 7.769, 'Denmark': 7.6, 'Norway': 7.554, 'Iceland': 7.494, 'Netherlands': 7.488}
Social Support: {'Finland': 1.587, 'Denmark': 1.573, 'Norway': 1.582, 'Iceland': 1.624, 
'Netherlands': 1.522}
Freedom to Make Life Choices: {'Finland': 0.596, 'Denmark': 0.592, 'Norway': 0.603, 'Iceland': 0.591, 'Netherlands': 0.557}
Healthy Life Expectancy: {'Finland': 0.986, 'Denmark': 0.996, 'Norway': 1.028, 'Iceland': 1.026, 'Netherlands': 0.999}
Perceptions of Corruption: {'Finland': 0.393, 'Denmark': 0.41, 'Norway': 0.341, 'Iceland': 0.118, 'Netherlands': 0.298}
GDP per Capita: {'Finland': 1.34, 'Denmark': 1.383, 'Norway': 1.488, 'Iceland': 1.38, 'Netherlands': 1.396}
Generosity: {'Finland': 0.153, 'Denmark': 0.252, 'Norway': 0.271, 'Iceland': 0.354, 'Netherlands': 0.322}
Country: {'Finland': 'Finland', 'Denmark': 'Denmark', 'Norway': 'Norway', 'Iceland': 'Iceland', 'Netherlands': 'Netherlands'}

EG 如果您想按国家/地区提取'Social Support'的分数:

print(dict_of_dicts["Social Support"])

输出:

{'Finland': 1.587, 'Denmark': 1.573, 'Norway': 1.582, 'Iceland': 1.624, 'Netherlands': 1.522}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM