[Numpy-discussion] Finding the same value in List

Todd toddrjen@gmail....
Wed Apr 17 05:34:45 CDT 2013


>
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130417/71b56c73/attachment.html 


More information about the NumPy-Discussion mailing list