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?

1

2 Answers 2

0

In this case you can do this:

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

More generally, look into collections.Counter.

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

Comments

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]

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.