使用迭代重命名python嵌套字典中的键和子键

使用迭代重命名python嵌套字典中的键和子键,python,dictionary,nested,Python,Dictionary,Nested,我正在尝试重命名python嵌套字典中的键和子键。然而,我还没有得到我预期的结果。下面是我拥有的原始嵌套密钥 nested_dict = { 0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56}, 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21}, 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93}, 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.

我正在尝试重命名python嵌套字典中的键和子键。然而,我还没有得到我预期的结果。下面是我拥有的原始嵌套密钥

nested_dict = {
 0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
 }
我正在尝试将键和子键更改为另一个值

 nested_dict = {
 4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56},
 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21},
 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93},
 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}
 }
我想到的是使用列表重命名密钥。我已尝试用以下列表替换键和子键:

new_key = []
for i in range(4,8):
    new_key.append(i)

然而,我仍然没有得到它。另一个想法是使用DataFrame重命名键和子键。我不确定使用列表或熊猫是否适合给定的问题。

用于从以下位置重命名密钥的代码:


用于从中重命名密钥的代码:

您可以使用(嵌套的)dict comprehension()。请注意,它会生成一个新的字典(但您可以将其分配给旧变量):

>>从pprint导入pprint作为pp
>>>
>>>嵌套的_dict={
...  0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
...  1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
...  2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
...  3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
...  }
>>>
>>>pp(嵌套目录)
{0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
>>>
>>>修改的_-nested_-dict={k0+4:{k1+4:v1表示k1,v0.items()中的v1}表示k0,v0表示nested_-dict.items()}
>>>
>>>pp(修改的嵌套目录)
{4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56},
5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21},
6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93},
7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}
您可以使用(嵌套的)dict comprehension()。请注意,它会生成一个新的字典(但您可以将其分配给旧变量):

>>从pprint导入pprint作为pp
>>>
>>>嵌套的_dict={
...  0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
...  1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
...  2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
...  3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
...  }
>>>
>>>pp(嵌套目录)
{0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
>>>
>>>修改的_-nested_-dict={k0+4:{k1+4:v1表示k1,v0.items()中的v1}表示k0,v0表示nested_-dict.items()}
>>>
>>>pp(修改的嵌套目录)
{4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56},
5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21},
6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93},
7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}

您可以为所需的任务使用Dataframe,如下所示:

import pandas as pd
nested_dict = {
            0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
            1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
            2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
            3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
            }
print("Dictionary before renaming: ", nested_dict)

# Convert nested dictionary to Pandas Dataframe
my_dataframe = pd.DataFrame.from_dict(nested_dict)

new_keys = list(range(4, 8)) # List of new keys
my_dataframe.columns = new_keys # Set columns to the new keys
my_dataframe.set_index([new_keys], inplace=True) # Set index to the new keys
nested_dict = my_dataframe.to_dict() # Convert back to nested dictionary

print("Dictionary after  renaming: ", nested_dict)
这将为您提供以下预期输出:

Dictionary before renaming:  {0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56}, 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21}, 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93}, 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
Dictionary after  renaming:  {4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56}, 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21}, 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93}, 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}

您可以为所需的任务使用Dataframe,如下所示:

import pandas as pd
nested_dict = {
            0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56},
            1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21},
            2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93},
            3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}
            }
print("Dictionary before renaming: ", nested_dict)

# Convert nested dictionary to Pandas Dataframe
my_dataframe = pd.DataFrame.from_dict(nested_dict)

new_keys = list(range(4, 8)) # List of new keys
my_dataframe.columns = new_keys # Set columns to the new keys
my_dataframe.set_index([new_keys], inplace=True) # Set index to the new keys
nested_dict = my_dataframe.to_dict() # Convert back to nested dictionary

print("Dictionary after  renaming: ", nested_dict)
这将为您提供以下预期输出:

Dictionary before renaming:  {0: {0: 33.97, 1: 55.32, 2: 57.31, 3: 71.56}, 1: {0: 27.31, 1: 23.32, 2: 32.25, 3: 60.21}, 2: {0: 65.38, 1: 36.88, 2: 70.88, 3: 21.93}, 3: {0: 35.44, 1: 21.21, 2: 40.72, 3: 51.35}}
Dictionary after  renaming:  {4: {4: 33.97, 5: 55.32, 6: 57.31, 7: 71.56}, 5: {4: 27.31, 5: 23.32, 6: 32.25, 7: 60.21}, 6: {4: 65.38, 5: 36.88, 6: 70.88, 7: 21.93}, 7: {4: 35.44, 5: 21.21, 6: 40.72, 7: 51.35}}