提取字典值的2D数组

时间:2018-02-20 12:22:28

标签: python arrays dictionary multidimensional-array list-comprehension

我有一个字典dic,其中包含一个键'Band_1'和一个二维值数组。我想简单地从字典中提取这些值作为2D数组,但我必须保持相同的索引/结构。字典看起来像:

{'Band_1': array([[ 72284.15387234,  71911.10445312,  76238.47771614, ...,
         43633.95847573,  45275.37592033,  45499.20557187],
       [ 70493.51666005,  70120.46724083,  72358.76375619, ...,
         40127.293935  ,  39381.19509655,  38709.70614194],
       [ 76238.47771614,  76238.47771614,  76238.47771614, ...,
         42738.63986959,  42440.20033421,  43037.07940497],
       ...,
       [ 45275.37592033,  45872.25499109,  45275.37592033, ...,
         25727.58635285,  24309.99855979,  24384.60844364],
       [ 47812.11197107,  50871.11720873,  49005.8701126 , ...,
         30875.66833818,  31845.59682817,  31770.98694433],
       [ 44007.00789496,  45499.20557187,  45349.98580418, ...,
         26622.904959  ,  28935.8113582 ,  31547.15729279]])}

到目前为止,我尝试过:

for key, values in dic.iteritems():
        for x in values[0]:
            array.append(x)
            for y in values[1]:
                array.append(y)

for keys, values in dic.items():
        array = [[y for y in values[1]] x for x in values[0]]

我需要一种有效的方法,因为数组会非常大吗?

1 个答案:

答案 0 :(得分:1)

你的“dic”变量包含数组,当你访问它时它是键,它会被返回...

array = dic['Band_1']

给你内部数组......

正如字典中的任何其他项目一样:

dict = {'key': 'value'}
dict['key'] #returns 'value'