【发布时间】:2016-01-04 23:20:59
【问题描述】:
import numpy as np
def gen_c():
c = np.ones(5, dtype=int)
j = 0
t = 10
while j < t:
c[0] = j
yield c.tolist()
j += 1
# What I did:
# res = np.array(list(gen_c())) <-- useless allocation of memory
# this line is what I'd like to do and it's killing me
res = np.fromiter(gen_c(), dtype=int) # dtype=list ?
错误说ValueError: setting an array element with a sequence.
这是一段非常愚蠢的代码。我想从生成器创建一个列表数组(最后是一个二维数组)...
虽然我到处搜索,但我仍然无法弄清楚如何让它工作。
【问题讨论】:
标签: python arrays numpy generator