Python 如何将嵌套字典转换为三维数组

Python 如何将嵌套字典转换为三维数组,python,list,dictionary,Python,List,Dictionary,我正在尝试在Python3中将嵌套字典转换为3D数组。我有一个来自另一篇文章的函数,它完成了将嵌套Dict转换为列表,如下所示 def Convert_Nest_Dictionary_2_List(Dictionary): local_list = [] for key, value in Dictionary.items(): local_list.append(key) local_list.extend(Convert_Nest_Dictio

我正在尝试在Python3中将嵌套字典转换为3D数组。我有一个来自另一篇文章的函数,它完成了将嵌套Dict转换为列表,如下所示

def Convert_Nest_Dictionary_2_List(Dictionary):
    local_list = []
    for key, value in Dictionary.items():
        local_list.append(key)
        local_list.extend(Convert_Nest_Dictionary_2_List(value))
    return local_list
当我使用此函数时,我得到错误:

`'numpy.ndarray' object has no attribute 'items'`
我假设这是因为嵌套字典中的对应值是具有形状的多维数组
(96144)
,而不是单个值

我的嵌套字典相当大,因此我将只在下面显示它的一部分。键入dict
RCP45
返回:

{'Sim1':{'01':{'2005':数组([[244.94081116244.95672607,…

使用3个sim卡,每个sim卡内有12个键(用于月),每个月有61个键(2005-2065表示年),每个对应值表示全球空间温度数据,作为
(96144)
阵列(纬度和经度)

我希望生成的数组是形状
(2196,94144)
,其中2196表示
3*12*61(模拟人生*月*年)

我将如何修改函数来实现这一点?或者可能一起使用不同的方法来实现这一点


非常感谢!

您需要一个基本大小写检查(当递归到达您的numpy数组时)

def Convert_Nest_Dictionary_2_List(Dictionary):
    local_list = []
    if isinstance(Dictionary, numpy.ndarray):
        return np_array_to_whatever_youre_trying_to_get(Dictionary)
    for key, value in Dictionary.items():
        local_list.append(key)
        local_list.extend(Convert_Nest_Dictionary_2_List(value))
    return local_list

尽管更改变量名可能是合适的。

抱歉,当我复制该位时,我使用了
.items()
函数,只需键入字典名
RCP45
返回
{Sim1':{01':{2005':数组([[244.94081116244.95672607,…
。我不熟悉使用嵌套字典,但如果我没有弄错的话,这是一个嵌套字典?是的,就是这样。尽管我仍然对数据结构叶上的2D数组(numpy.ndarray of shape(96144))感到困惑。我本以为这会使数据变成4D而不是3D。