[英]How to convert key to value in dictionary type?
I have a question about the convert key.我有一个关于转换键的问题。
First, I have this type of word count in Data Frame.首先,我在数据框中有这种类型的字数。
[Example] [例子]
dict = {'forest': 10, 'station': 3, 'office': 7, 'park': 2}
I want to get this result.我想得到这个结果。
[Result] [结果]
result = {'name': 'forest', 'value': 10,
'name': 'station', 'value': 3,
'name': 'office', 'value': 7,
'name': 'park', 'value': 2}
Please check this issue.请检查此问题。
As Rakesh said:正如拉克什所说:
dict cannot have duplicate keys dict 不能有重复的键
The closest way to achieve what you want is to build something like that实现您想要的最接近的方法是构建类似的东西
my_dict = {'forest': 10, 'station': 3, 'office': 7, 'park': 2}
result = list(map(lambda x: {'name': x[0], 'value': x[1]}, my_dict.items()))
You will get你会得到
result = [
{'name': 'forest', 'value': 10},
{'name': 'station', 'value': 3},
{'name': 'office', 'value': 7},
{'name': 'park', 'value': 2},
]
As Rakesh said, You can't have duplicate values in the dictionary You can simply try this.正如 Rakesh 所说,您不能在字典中有重复的值您可以简单地尝试一下。
dict = {'forest': 10, 'station': 3, 'office': 7, 'park': 2}
result = {}
count = 0;
for key in dict:
result[count] = {'name':key, 'value': dict[key]}
count = count + 1;
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.