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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.