1

Okay so I am fairly new to python and numpy, what I want to do is take a single array of randomly generated integers and check to see if there are multiple occurrences of each number for example if b=numpy.array([3,2,33,6,6]) it would then tell me that 6 occurs twice. or if a=numpy.array([22,21,888]) that each integer is different.

Topias
  • 11
  • 2

1 Answers1

0

You can use counter to check how may times a given number occurs in a list or any iterable object:

In [2]: from collections import Counter

In [8]: b=numpy.array([3,2,33,6,6])

In [9]: Counter(b)
Out[9]: c = Counter({6: 2, 33: 1, 2: 1, 3: 1})

In [14]: c.most_common(1)
Out[14]: [(6, 2)] # this tells you what is most common element 
                  # if instead of 2 you have 1, you know all elements 
                  # are different.

Similarly you can do for your second example:

In [15]: a=numpy.array([22,21,888])

In [16]: Counter(a)
Out[16]: Counter({888: 1, 21: 1, 22: 1})

Other way is to use set, and compare resulting set length with length of your array.

In [20]: len(set(b)) == len(b)
Out[20]: False

In [21]: len(set(a)) == len(a)
Out[21]: True

Hope this helps.

Marcin
  • 215,873
  • 14
  • 235
  • 294