我正在尝试替换类似的代码,以便使用numpy进行矢量化的高效操作。
counter = 0
idxs = [1, 3]
lists = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
for l in lists:
for idx in idxs:
counter += l[idx]
发布于 2016-11-15 22:45:01
只需将数组之和:
idxs = [1, 3]
lists = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
ary = np.array(lists)
counter = ary[:,idxs].sum()
https://stackoverflow.com/questions/40620999
复制相似问题