在此示例中,我想知道numpy.fromiter
是否在评估期间创建了列表。我想要的是“懒惰”评估,其中使用迭代器避免分配/取消分配列表。请注意,示例函数是一个距离度量标准,输出是一个点与一组点之间的距离的列表。这不是问题所在,仅是示例。另外,我确实需要使用product
,因为在实际情况下它将是itr.product(xyz, repeat=2)
,这得到了简化。
import numpy as np
import itertools as itr
xyz = np.array([[0,0,0], [75,75,25], [50,75,100], [0, 50, 0]], np.int)
wide = len(xyz)
p = np.array([[50, 50, 50]], np.int)
np.fromiter(map(lambda y:
np.sum(np.abs(p - y)), itr.product(xyz, repeat=1)),
np.int, wide)
输出array([150, 75, 75, 100])