Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要在 numpy 中创建一个向量 z,使得 z[i] = 1/(1+i)。那么在下面的代码中有没有更快的方法呢?
import numpy as np n = 10000 z = np.zeros(n) for i in xrange(n): z[i] = 1.0/(1 + i)
Quickest way:
z = 1.0/np.arange(1, n+1)
怎么样:
z = np.arange(n) z = 1/(1+z)