【发布时间】:2019-01-29 18:24:46
【问题描述】:
我有这个代码:
import numpy as np
result = {}
result['depth'] = [1,1,1,2,2,2]
result['generation'] = [1,1,1,2,2,2]
result['dimension'] = [1,2,3,1,2,3]
result['data'] = [np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0]), np.array([0,0,0])]
for v in np.unique(result['depth']):
temp_v = np.where(result['depth'] == v)
values_v = [result[string][temp_v] for string in result.keys()]
this_v = dict(zip(result.keys(), values_v))
我想在其中创建一个名为“this_v”的新dict,其键与原始字典result 相同,但值更少。
行:
values_v = [result[string][temp_v] for string in result.keys()]
报错
TypeError:列表索引必须是整数,而不是元组
我不明白,因为我可以创建 ex = result[result.keys()[0]][temp_v] 就好了。它只是不允许我使用 for 循环来执行此操作,以便我可以填充列表。
知道为什么它不起作用吗?
【问题讨论】:
-
np.where返回一个元组
标签: python numpy dictionary