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?
Asked
Active
Viewed 59 times
0
-
Duplicate of https://stackoverflow.com/q/2600191/929999 – Torxed Jun 16 '20 at 12:47
2 Answers
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