我想看看是否有人可以提出解决我遇到的问题的想法。
我想尝试以某种方式翻转字典的子列表和列表。以下内容应介绍我的目标。
olddict = {
'Cat1': {
'a': 0.9628883469409395,
'b': 0.2215033887861984,
'c': 0.011300919842312747,
'd': 0.006507874528179097,
'e': 0.0038535645472061657},
'Cat2': {'a': 0.9984095005831831,
'b': 0.7831176833025262,
'c': 0.2980289093298292,
'd': 0.04152023948978264,
'e': 0.9772639691714836},
'Cat3': {'a': 0.467005076142132,
'b': 1.0,
'c': 0.9810315430520035,
'd': 0.37869041728517233,
'e': 0.5812393632996296}}
变成这样:
newdict = {
'a': {
'Cat1': 0.9628883469409395,
'Cat2': 0.9984095005831831,
'Cat3': 0.467005076142132},
'b': {
'Cat1': 0.2215033887861984,
'Cat2': 0.7831176833025262,
'Cat3': 1.0},
'c': {
'Cat1': 0.011300919842312747,
'Cat2': 0.2980289093298292,
'Cat3': 0.9810315430520035},
'd': {
'Cat1': 0.9628883469409395,
'Cat2': 0.04152023948978264,
'Cat3': 0.37869041728517233},
'e': {
'Cat1': 0.0038535645472061657,
'Cat2': 0.9772639691714836,
'Cat3': 0.5812393632996296}}
以此,我可以创建新字典的总体布局。但是,我也未能添加值。
newdict = {}
for key, value in olddict.items():
for string,v in value.items():
newdict.setdefault(string, []).append(key)
我将不胜感激。提前非常感谢!
答案 0 :(得分:3)
newdict = defaultdict(dict)
for cat, letters in olddict.items():
for letter, value in letters.items():
newdict[letter][cat] = value
答案 1 :(得分:2)
尝试使用defaultdict
。他们太神奇了。
https://docs.python.org/3/library/collections.html#collections.defaultdict
>>> from collections import defaultdict
>>> from pprint import pprint
>>>
>>> newdict = defaultdict(dict)
>>> for key, value in olddict.items():
... for string, v in value.items():
... newdict[string][key] = v
>>>
>>> pprint(newdict)
defaultdict(<class 'dict'>,
{'a': {'Cat1': 0.9628883469409395,
'Cat2': 0.9984095005831831,
'Cat3': 0.467005076142132},
'b': {'Cat1': 0.2215033887861984,
'Cat2': 0.7831176833025262,
'Cat3': 1.0},
'c': {'Cat1': 0.011300919842312747,
'Cat2': 0.2980289093298292,
'Cat3': 0.9810315430520035},
'd': {'Cat1': 0.006507874528179097,
'Cat2': 0.04152023948978264,
'Cat3': 0.37869041728517233},
'e': {'Cat1': 0.0038535645472061657,
'Cat2': 0.9772639691714836,
'Cat3': 0.5812393632996296}})
答案 2 :(得分:1)
import pandas as pd
olddict = {'Cat1': {'a': 0.9628883469409395,
'b': 0.2215033887861984,
'c': 0.011300919842312747,
'd': 0.006507874528179097,
'e': 0.0038535645472061657},
'Cat2': {'a': 0.9984095005831831,
'b': 0.7831176833025262,
'c': 0.2980289093298292,
'd': 0.04152023948978264,
'e': 0.9772639691714836},
'Cat3': {'a': 0.467005076142132,
'b': 1.0,
'c': 0.9810315430520035,
'd': 0.37869041728517233,
'e': 0.5812393632996296}}
df=pd.DataFrame(olddict)
df = df.T
df.to_dict()
输出:
{'a': {'Cat1': 0.9628883469409395,
'Cat2': 0.9984095005831831,
'Cat3': 0.467005076142132},
'b': {'Cat1': 0.2215033887861984, 'Cat2': 0.7831176833025262, 'Cat3': 1.0},
'c': {'Cat1': 0.011300919842312747,
'Cat2': 0.2980289093298292,
'Cat3': 0.9810315430520035},
'd': {'Cat1': 0.006507874528179097,
'Cat2': 0.04152023948978264,
'Cat3': 0.37869041728517233},
'e': {'Cat1': 0.0038535645472061657,
'Cat2': 0.9772639691714836,
'Cat3': 0.5812393632996296}}
答案 3 :(得分:0)
您使用setdefault
的解决方案几乎是正确的,但是您使用的是字典,而不是列表,因此您应该这样做:
olddict = {'Cat1': {'a': 0.9628883469409395,
'b': 0.2215033887861984,
'c': 0.011300919842312747,
'd': 0.006507874528179097,
'e': 0.0038535645472061657},
'Cat2': {'a': 0.9984095005831831,
'b': 0.7831176833025262,
'c': 0.2980289093298292,
'd': 0.04152023948978264,
'e': 0.9772639691714836},
'Cat3': {'a': 0.467005076142132,
'b': 1.0,
'c': 0.9810315430520035,
'd': 0.37869041728517233,
'e': 0.5812393632996296}}
expected = {'a': {'Cat1': 0.9628883469409395,
'Cat2': 0.9984095005831831,
'Cat3': 0.467005076142132},
'b': {'Cat1': 0.2215033887861984,
'Cat2': 0.7831176833025262,
'Cat3': 1.0},
'c': {'Cat1': 0.011300919842312747,
'Cat2': 0.2980289093298292,
'Cat3': 0.9810315430520035},
'd': {'Cat1': 0.006507874528179097,
'Cat2': 0.04152023948978264,
'Cat3': 0.37869041728517233},
'e': {'Cat1': 0.0038535645472061657,
'Cat2': 0.9772639691714836,
'Cat3': 0.5812393632996296}}
newdict = {}
for key, value in olddict.items():
for s, v in value.items():
newdict.setdefault(s, {})[key] = v
print(newdict == expected)
# True