[Numpy-discussion] Finding the same value in List

Todd toddrjen at gmail.com
Wed Apr 17 04:46:17 EDT 2013


x,i=numpy.unique(y, return_inverse=True)
f=[numpy.where(i==ind) for ind in range(len(x))]

x will give you the list of unique values, and f will give you the indices
of each corresponding value in x.  So f[0] is the indices of x[0] in y.

To explain, unique in this form gives two outputs, a sorted, non-repeating
list of values (x), and an array of the same shape as y that gives you the
indices of x of each corresponding value of y (i, that is x[i] is the same
as y)

The second goes through each index of x and finds where that index occurs
in i.


On Wed, Apr 17, 2013 at 9:44 AM, Happyman <bahtiyor_zohidov at mail.ru> wrote:

> Hello,
>
> I have had encountered some problem while I was trying to create the
> following code which finds number of the same values  and indexes in an
> array or list. Here is the code:
>
> y = [ 1, 12,  3,  3,  5,  1,  1, 34, 0, 0,  1,  5]
> OR
> y = array( [ 1, 12,  3,  3,  5,  1,  1, 34, 0, 0,  1,  5 ] )
>
> b = [ [ item for item in range( len(y) ) if y[ item ] == y[ j ] ]  for j
> in range(0, len( y ) ) ]
>
> answer:
>    [ [ 0, 5, 6, 10], [1], [2, 3], [2, 3], [4, 11], [0, 5, 6, 10], [0, 5,
> 6, 10], [7], [8, 9], [8, 9], [0, 5, 6, 10], [4, 11] ]
>
> The result I want to get is ,not that shown above as an answer, that I
> want to calculate the number of the same values and their indexes in not
> repeated way as well. For example,
> '1' - 4, index: '0, 5, 6, 10'
> '12' - 1, index: '1'
> '3' - 2, index: '2, 3'
> '5' - 2, index: '4, 11'
> '34' - 1, index: '7'
> '0' - 2, index: '8, 9'
>
> Any answer would be appreciated..
>
>
>
>
>
> --
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130417/6ab4cd8e/attachment.html>


More information about the NumPy-Discussion mailing list