This is an excerpt from a documentation.
lambda ind, r: 1.0 any(np.array(points_2d)[ind][:,0] == 0.0)
But I don't understand np.array(points_2d)[ind][:,0]
.
It seems equivalent to myarray[0][:,0]
, which doesn't make sense to me.
Can anyone help to explain?
CodePudding user response:
With points_2d
from earlier in the doc:
In [38]: points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
...: (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
In [39]: np.array(points_2d)
Out[39]:
array([[0. , 0. ],
[0. , 1. ],
[1. , 1. ],
[1. , 0. ],
[0.5 , 0.25],
[0.5 , 0.75],
[0.25, 0.5 ],
[0.75, 0.5 ]])
Indexing with a scalar gives a 1d array, which can't be further indexed with [:,0]
.
In [40]: np.array(points_2d)[0]
Out[40]: array([0., 0.])
But with a list or slice:
In [41]: np.array(points_2d)[[0,1,2]]
Out[41]:
array([[0., 0.],
[0., 1.],
[1., 1.]])
In [42]: np.array(points_2d)[[0,1,2]][:,0]
Out[42]: array([0., 0., 1.])
So this selects the first column of a subset of rows.
In [43]: np.array(points_2d)[[0,1,2]][:,0]==0.0
Out[43]: array([ True, True, False])
In [44]: any(np.array(points_2d)[[0,1,2]][:,0]==0.0)
Out[44]: True
I think they could have used:
In [45]: np.array(points_2d)[[0,1,2],0]
Out[45]: array([0., 0., 1.])