[Numpy-discussion] Finding the same value in List
Wed Apr 17 09:25:46 CDT 2013
At bit OT, but I am new to numpy. The help for np.where says:
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
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
In : np.where(i==0)
Out: (array([8, 9]),)
This should just return the ndarray and not the tuple of ndarrays. In what
case does it only return the ndarray?
On Wed, Apr 17, 2013 at 4:34 AM, Todd <firstname.lastname@example.org> wrote:
> The data type:
>> x in ndarray and x[ i ]--> int64
>> type(f) --> ' list '
>> type( f[ 0 ] ) --> ' tuple '
>> type( f[ 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() instead of just where() as it was in my first example):
> x,i=numpy.unique(y, return_inverse=True)
> f=[numpy.where(i==ind) for ind in range(len(x))]
> type(f) --> list
> type(f) --> ndarray
> type(f) 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 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion