【发布时间】:2016-05-14 12:03:15
【问题描述】:
我正在尝试获取 numpy 数组中特定字典值的向量。这是数组的样子:
import numpy as np
edge_array = np.array(
[[1001, 7005, {'lanes': 9, 'length': 0.35, 'type': '99', 'modes': 'cw'}],
[1001, 8259, {'lanes': 10, 'length': 0.46, 'type': '99', 'modes': 'cw'}],
[1001, 14007, {'lanes': 7, 'length': 0.49, 'type': '99', 'modes': 'cw'}]])
每行的前两个值都有一个向量(即1001 和7005,但我需要另一个向量来存储与'lanes' 关联的值。
到目前为止,这是我的代码:
row_idx = edge_array[:, 0]
col_idx = edge_array[:, 1]
lane_values = edge_array[:, 2['lanes']]
我得到的错误如下:
lane_values = edge_array[:, 2['lanes']]
TypeError: 'int' object has no attribute '__getitem__'
如果您需要任何进一步的说明,请告诉我,谢谢!
【问题讨论】:
-
注意这个数组的
dtype是object。edge_array[:,2]在功能上与字典列表相同。所以你必须使用列表操作(映射、理解)来对各个字典进行操作。
标签: python numpy dictionary sparse-matrix