Why some ndarray objects cannot be indexed

In [23]:  sa_12 = np.array( set(n_1) & set(n_2) )

In [24]: sa_12[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-a1610487c9ce> in <module>
----> 1 sa_12[0]

IndexError: too many indices for array

In [25]: sa_12
Out[25]: array({9, 3, 15}, dtype=object)

In [26]: print(sa_12)
{9, 3, 15}

In [ 27 ]: sa_12( 0 )
 ----------------------------------------- ---------------------------------- 
TypeError Traceback (most recent call  last )
<ipython-input-27-417ff62b5c0d> in <module>
----> 1 sa_12(0)

TypeError: 'numpy.ndarray' object is not callable

In [ 28 ]: np.in1d(n_1,n_2)
 Out [ 28 ]:
 array ([ False ,   True , False , False ,   True , False , False ,   True , False ,
        False ])

In [29]: n_1[np.in1d(n_1,n_2)]
Out[29]: array([ 3,  9, 15])

In [30]: sm = n_1[np.in1d(n_1,n_2)]

In [31]: type(sm)
Out[31]: numpy.ndarray

In [32]: sm[0]
Out[32]: 3

In [33]: type(sa_12)
Out[33]: numpy.ndarray

In [34]: sa_12[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-34-a1610487c9ce> in <module>
----> 1 sa_12[0]

IndexError: too many indices for array

And some can,

I know, the original reason is that even if the objects of the same type are [numpy.ndarray] , there are still internal differences

In [31]: type(sm)
Out[31]: numpy.ndarray

In [32]: sm[0]
Out[32]: 3

In [33]: type(sa_12)
Out[33]: numpy.ndarray

In [35]: sm
Out[35]: array([ 3,  9, 15])

In [36]: sa_12
Out[36]: array({9, 3, 15}, dtype=object)

sm is what I converted from the list object

And sa__21 is converted from set

Leave a Comment

Your email address will not be published. Required fields are marked *