[英]How can i convert my python dictionary to below format
my dictionary: 我的字典:
[
{
"male_Unmarried": 792,
"male_Husband": 13192,
"male_own_child": 2823,
"male_not_in_family": 4430,
"male_wife": 2,
}
]
expecting: 期待:
[
{
"label": "male_Unmarried",
"value": 792
},
{
"label": "male_Husband",
"value": 13192
},
{
"label": "male_own_child",
"value": 2823
},
{
"label": "male_not_in_family",
"value": 4430
},
{
"label": "male_wife",
"value": 2
}
]
how can i convert first dictionay like second one. 我怎样才能像第二个一样转换第一个字典。
I want for each dictionay of first one the key will be label and value will be value of first dictonary. 我想对于第一个字典的每个字典,键将是标签,而值将是第一个字典的值。
Please have a look 请看一看
You could use a list-comprehension: 您可以使用列表理解:
lst = [
{
"male_Unmarried": 792,
"male_Husband": 13192,
"male_own_child": 2823,
"male_not_in_family": 4430,
"male_wife": 2,
}
]
print([{'label': k, 'value': v} for k, v in lst[0].items()])
# [{'label': 'male_Unmarried', 'value': 792},
# {'label': 'male_Husband', 'value': 13192},
# {'label': 'male_own_child', 'value': 2823},
# {'label': 'male_not_in_family', 'value': 4430},
# {'label': 'male_wife', 'value': 2}]
lst=[ {"male_Unmarried": 792,"male_Husband": 13192,"male_own_child": 2823,
"male_not_in_family": 4430,"male_wife": 2,} ]
lst_new = [{"label":i,"value":a[0][i]} for i in lst[0]]
lst_new
[{'label': 'male_Unmarried', 'value': 792},
{'label': 'male_wife', 'value': 2},
{'label': 'male_own_child', 'value': 2823},
{'label': 'male_not_in_family', 'value': 4430},
{'label': 'male_Husband', 'value': 13192}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.