重新组织嵌套的 `dict`

huangapple go评论65阅读模式
英文:

Reorganize nested `dict`

问题

这个问题与此处相关。我想要重新组织以下嵌套的dict

a = {
 (0.0, 0.0): {'a': [25, 29, nan]},
 (0.0, 2.0): {'a': [25, 29, nan], 'b': [25, 35, 31.0]},
 (0.0, 4.0): {'b': [25, 35, 31.0]},
 (2.0, 0.0): {'a': [25, 29, nan], 'c': [25, 26, 29.0]},
 (2.0, 1.5): {'a': [25, 29, nan], 'c': [25, 26, 29.0]},
 (2.0, 2.0): {'a': [25, 29, nan], 'b': [25, 35, 31.0]},
 (2.0, 4.0): {'b': [25, 35, 31.0]},
 (3.0, 3.0): {'d': [25, 31, 32.0]},
 (3.0, 5.0): {'d': [25, 31, 32.0]},
 (5.0, 0.0): {'c': [25, 26, 29.0]},
 (5.0, 1.5): {'c': [25, 26, 29.0]},
 (5.0, 3.0): {'d': [25, 31, 32.0]},
 (5.0, 5.0): {'d': [25, 31, 32.0]},
 (6.0, 1.0): {'e': [25, 28, 30.0]},
 (6.0, 3.0): {'e': [25, 28, 30.0]},
 (8.0, 1.0): {'e': [25, 28, 30.0]},
 (8.0, 3.0): {'e': [25, 28, 30.0]}
}

我想要交换内部和外部的key。一些外部的key会重复,value 应该变成一个listlists。结果应该是:

{'a': {(0.0, 0.0): [[25, 29, nan]],
       (0.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (2.0, 0.0): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 1.5): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 2.0): [[25, 29, nan], [25, 35, 31.0]]},
 'b': {(0.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (0.0, 4.0): [[25, 35, 31.0]],
       (2.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (2.0, 4.0): [[25, 35, 31.0]]},
 'c': {(2.0, 0.0): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 1.5): [[25, 29, nan], [25, 26, 29.0]],
       (5.0, 0.0): [[25, 26, 29.0]],
       (5.0, 1.5): [[25, 26, 29.0]]},
 'd': {(3.0, 3.0): [[25, 31, 32.0]],
       (3.0, 5.0): [[25, 31, 32.0]],
       (5.0, 3.0): [[25, 31, 32.0]],
       (5.0, 5.0): [[25, 31, 32.0]]},
 'e': {(6.0, 1.0): [[25, 28, 30.0]],
       (6.0, 3.0): [[25, 28, 30.0]],
       (8.0, 1.0): [[25, 28, 30.0]],
       (8.0, 3.0): [[25, 28, 30.0]]}
}

直觉告诉我使用pd.DataFrame.groupby() [并剔除NaN单元格] 是正确的方法...

df = pd.DataFrame(dict_vertices)
print(df.head(2))
             0.0               2.0                    ...  8.0       6.0
             0.0               0.0               1.5  ...  1.0  3.0  3.0
a  [25, 29, nan]     [25, 29, nan]     [25, 29, nan]  ...  NaN  NaN  NaN
c            NaN  [[25, 26, 29.0]]  [[25, 26, 29.0]]  ...  NaN  NaN  NaN

[2 rows x 17 columns]

...但我不确定。如何重新组织以下嵌套的dict,其中value跟随外部的key

英文:

This question is connected to [-> here].
I would like to reorganize the following nested dict please:

a = {
 (0.0, 0.0): {'a': [25, 29, nan]},
 (0.0, 2.0): {'a': [25, 29, nan], 'b': [25, 35, 31.0]},
 (0.0, 4.0): {'b': [25, 35, 31.0]},
 (2.0, 0.0): {'a': [25, 29, nan], 'c': [25, 26, 29.0]},
 (2.0, 1.5): {'a': [25, 29, nan], 'c': [25, 26, 29.0]},
 (2.0, 2.0): {'a': [25, 29, nan], 'b': [25, 35, 31.0]},
 (2.0, 4.0): {'b': [25, 35, 31.0]},
 (3.0, 3.0): {'d': [25, 31, 32.0]},
 (3.0, 5.0): {'d': [25, 31, 32.0]},
 (5.0, 0.0): {'c': [25, 26, 29.0]},
 (5.0, 1.5): {'c': [25, 26, 29.0]},
 (5.0, 3.0): {'d': [25, 31, 32.0]},
 (5.0, 5.0): {'d': [25, 31, 32.0]},
 (6.0, 1.0): {'e': [25, 28, 30.0]},
 (6.0, 3.0): {'e': [25, 28, 30.0]},
 (8.0, 1.0): {'e': [25, 28, 30.0]},
 (8.0, 3.0): {'e': [25, 28, 30.0]}
}

I want to swap the inner and outer keys.
Some outer keys will duplicate and the value should become a list of lists. The result should be:

{'a': {(0.0, 0.0): [[25, 29, nan]],
       (0.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (2.0, 0.0): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 1.5): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 2.0): [[25, 29, nan], [25, 35, 31.0]]},
 'b': {(0.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (0.0, 4.0): [[25, 35, 31.0]],
       (2.0, 2.0): [[25, 29, nan], [25, 35, 31.0]],
       (2.0, 4.0): [[25, 35, 31.0]]},
 'c': {(2.0, 0.0): [[25, 29, nan], [25, 26, 29.0]],
       (2.0, 1.5): [[25, 29, nan], [25, 26, 29.0]],
       (5.0, 0.0): [[25, 26, 29.0]],
       (5.0, 1.5): [[25, 26, 29.0]]},
 'd': {(3.0, 3.0): [[25, 31, 32.0]],
       (3.0, 5.0): [[25, 31, 32.0]],
       (5.0, 3.0): [[25, 31, 32.0]],
       (5.0, 5.0): [[25, 31, 32.0]]},
 'e': {(6.0, 1.0): [[25, 28, 30.0]],
       (6.0, 3.0): [[25, 28, 30.0]],
       (8.0, 1.0): [[25, 28, 30.0]],
       (8.0, 3.0): [[25, 28, 30.0]]}
}

Intuition tells me pd.DataFrame with a .groupby() [and cull the NaN cells] would be the way to go...

df = pd.DataFrame(dict_vertices)
print(df.head(2))
             0.0               2.0                    ...  8.0       6.0
             0.0               0.0               1.5  ...  1.0  3.0  3.0
a  [25, 29, nan]     [25, 29, nan]     [25, 29, nan]  ...  NaN  NaN  NaN
c            NaN  [[25, 26, 29.0]]  [[25, 26, 29.0]]  ...  NaN  NaN  NaN

[2 rows x 17 columns]

...but I don't know.
How do I reorganize the following nested dict please; where the value follows the outer key?

答案1

得分: 1

你可以使用以下代码:

out = {}

for k1, d in a.items():
    for k2 in d:
        out.setdefault(k2, {})[k1] = list(d.values())

输出如下:

{
    'a': {
        (0.0, 0.0): [[25, 29, nan]],
        (0.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
        (2.0, 0.0): [[25, 29, nan], [[25, 26, 29.0]]],
        (2.0, 1.5): [[25, 29, nan], [[25, 26, 29.0]]],
        (2.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]]
    },
    'b': {
        (0.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
        (0.0, 4.0): [[25, 35, 31.0]],
        (2.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
        (2.0, 4.0): [[25, 35, 31.0]]
    },
    'c': {
        (2.0, 0.0): [[25, 29, nan], [[25, 26, 29.0]]],
        (2.0, 1.5): [[25, 29, nan], [[25, 26, 29.0]]],
        (5.0, 0.0): [[25, 26, 29.0]],
        (5.0, 1.5): [[25, 26, 29.0]]
    },
    'd': {
        (3.0, 3.0): [[25, 31, 32.0]],
        (3.0, 5.0): [[25, 31, 32.0]],
        (5.0, 3.0): [[25, 31, 32.0]],
        (5.0, 5.0): [[25, 31, 32.0]]
    },
    'e': {
        (6.0, 1.0): [[25, 28, 30.0]],
        (6.0, 3.0): [[25, 28, 30.0]],
        (8.0, 1.0): [[25, 28, 30.0]],
        (8.0, 3.0): [[25, 28, 30.0]]
    }
}
英文:

You can use:

out = {}

for k1, d in a.items():
    for k2 in d:
        out.setdefault(k2, {})[k1] = list(d.values())

Output:

{'a': {(0.0, 0.0): [[25, 29, nan]],
       (0.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
       (2.0, 0.0): [[25, 29, nan], [[25, 26, 29.0]]],
       (2.0, 1.5): [[25, 29, nan], [[25, 26, 29.0]]],
       (2.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]]},
 'b': {(0.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
       (0.0, 4.0): [[25, 35, 31.0]],
       (2.0, 2.0): [[25, 29, nan], [[25, 35, 31.0]]],
       (2.0, 4.0): [[25, 35, 31.0]]},
 'c': {(2.0, 0.0): [[25, 29, nan], [[25, 26, 29.0]]],
       (2.0, 1.5): [[25, 29, nan], [[25, 26, 29.0]]],
       (5.0, 0.0): [[25, 26, 29.0]],
       (5.0, 1.5): [[25, 26, 29.0]]},
 'd': {(3.0, 3.0): [[25, 31, 32.0]],
       (3.0, 5.0): [[25, 31, 32.0]], 
       (5.0, 3.0): [[25, 31, 32.0]],
       (5.0, 5.0): [[25, 31, 32.0]]},
 'e': {(6.0, 1.0): [[25, 28, 30.0]],
       (6.0, 3.0): [[25, 28, 30.0]],
       (8.0, 1.0): [[25, 28, 30.0]],
       (8.0, 3.0): [[25, 28, 30.0]]},
}

huangapple
  • 本文由 发表于 2023年6月2日 00:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定