且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Numpy 有效地创建条件列数组?

更新时间:2023-11-11 09:19:28

这样的事情有用吗?

tot_length=200
steps=0.1
list_no = np.arange(0.0, tot_length, steps)
x, y, z = np.meshgrid(*[list_no for _ in range(3)], sparse=True)
a = ((x>=y) & (y>=z)).nonzero()

这仍然会为中间布尔数组使用 8GB 的​​内存,但避免重复调用 np.append,因为它们很慢.

This will still use 8GB of memory for the intermediate array of booleans, but avoids repeated calls to np.append which are slow.