1

我有一个空的“numpy.ndarray”

import numpy as np
my_grid =  np.zeros((5, 5))

parse = "max","min","avg"

我希望创建一个字典,其中 parse 的每个元素都是“键”

从集合导入 defaultdict

GridMetric = dict()
for arg in parse:
    GridMetric[arg].append(my_grid)

但我得到这个错误

   Traceback (most recent call last):
  File "<editor selection>", line 3, in <module>
KeyError: 'max'
4

2 回答 2

3

如果您想要的是一个字典,其键是称为 parse 的列表的不同元素,并且其值都是相同的数组,那么对您的代码进行以下更改应该可以工作:

import numpy as np
my_grid =  np.zeros((5, 5))

parse = ["max","min","avg"]

d = {}
for arg in parse:
    d[arg] = my_grid
于 2013-03-03T17:40:31.597 回答
1

试试这个:

import numpy as np

my_grid =  np.zeros((5, 5))

parse = ["max","min","avg"]


for arg in parse:
    dict(parse=my_grid)

print(d)
于 2019-07-17T13:25:52.690 回答