I have a list, say
a = [3, 4, 5, 6, 7]
And i want to create a numpy array of zeros of that list's length.
If I do
b = np.zeros((len(a), 1))
I get
[[0, 0, 0, 0, 0]]
instead of
[0, 0, 0, 0, 0]
What is the best way to get the latter option?
If you don't want to have to care about shapes, use np.zeros_like:
np.zeros_like(a)
# array([0, 0, 0, 0, 0])
There's also the option of querying np.shape:
np.zeros(np.shape(a))
# array([0., 0., 0., 0., 0.])
Both options should work for ND lists as well.
np.zeros with the correct shape. So my first option was to give you something that did not need explicit dealing with the shape at all—let numpy handle that.
b = np.zeros((len(a), 1))[0]