0

If I have an array, a = [1,2,3,4,2,1], how can I create another array, which shows the number of times each number in array a has been repeated, for example from array a then the new array would be b = [2,2,1,1]? Is this possible using a command in the NumPy library?

Torxed
  • 22,866
  • 14
  • 82
  • 131
Evelyn
  • 19
  • 2

2 Answers2

0

In this case you can do this:

[a.count(i + 1) for i in range(max(a))]

More generally, look into collections.Counter.

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
0

I'm certain there are many ways to do that. Here's one:

a = [1,2,3,4,2,1]

count = {}
for item in a: 
    count[item] = count.get(item, 0) + 1

[v for k,v in count.items()]

Result:

[2, 2, 1, 1]
Roy2012
  • 11,755
  • 2
  • 22
  • 35