Your array a = numpy.array([1,2])
only has one dimension: its shape is (2,)
. However, your slice a[:,0]
specifies selections for two dimensions. This causes NumPy to raise the error.
To get the first element from a
you only need to write a[0]
(a selection for only one dimension is being made here).
Looking at your other question, if you want to ensure that the syntax a[:,0]
always works, you could ensure that a
always has two dimensions. When loading an array with np.loadtxt
use the ndmin
parameter, e.g.:
np.loadtxt(F, skiprows=0, ndmin=2)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…