我有一个词典列表,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}
如果出于某种原因,您想创建带有国家名称和EACH关键字值的单独词典,您可以这样做:
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}]
如果你更喜欢这种神秘的列表理解,而不是被忽视:
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}