[Numpy-discussion] Finding the same value in List

Bob Nnamtrop bob.nnamtrop@gmail....
Wed Apr 17 09:25:46 CDT 2013


At bit OT, but I am new to numpy. The help for np.where says:

  Returns
    -------
    out : ndarray or tuple of ndarrays
        If both `x` and `y` are specified, the output array contains
        elements of `x` where `condition` is True, and elements from
        `y` elsewhere.

        If only `condition` is given, return the tuple
        ``condition.nonzero()``, the indices where `condition` is True.

However, I don't see any case that it returns an ndarray (it always seems
to return a tuple of ndarrys). It seems to me for the case where only
'condition' is given it should return just the ndarry, eg (using this case
discussed above):

In [44]: np.where(i==0)
Out[44]: (array([8, 9]),)

This should just return the ndarray and not the tuple of ndarrays. In what
case does it only return the ndarray?

Thanks,
Bob



On Wed, Apr 17, 2013 at 4:34 AM, Todd <toddrjen@gmail.com> wrote:

> The data type:
>> x in ndarray and x[ i ]--> int64
>> type(f)                     -->   ' list '
>> type( f[ 0 ] )             -->   ' tuple '
>> type( f[ 0][0] )          -->  'ndarray'
>> type( f[ 0 ][ 0 ][ 0]  ) -->  'int64'
>>
>> How do you think to avoid diversity if data type in this example? I think
>>  it is not necessary to get diverse dtype as well as more than 1D array..
>>
>
> That is why I suggested this approach was better ( note the that this is
> where()[0] instead of just where() as it was in my first example):
>
> x,i=numpy.unique(y, return_inverse=True)
> f=[numpy.where(i==ind)[0] for ind in range(len(x))]
>
> type(f)     --> list
> type(f[0]) --> ndarray
>
> type(f[0][0]) is meaningless since it is just a single element in an
> array.  It must be an int type of some sort of since indices have to be int
> types.  x will be the same dtype as your input array.
>
> You could conceivably change the type of f[0] to a list, but why would you
> want to?  One of the big advantages of python is that usually it doesn't
> matter what the type is.  In this case, a numpy ndarray will work the same
> as a list in most cases where you would want to use these sorts of indices.
> It is possibly to change the ndarray to a list, but unless there is a
> specific reason you need to use lists so then it is better not to.
>
> You cannot change the list to an ndarray because the elements of the list
> are different lengths.  ndarray doesn't support that.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130417/203bee57/attachment-0001.html 


More information about the NumPy-Discussion mailing list