Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add count for each unique val in list

Assume I have a list.

temp = ['A', 'B', 'A', 'B', 'A', 'B']

I am looking for a way to join the count of the string inside.

Intended Output:

['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']

I was able to solve it by using a list comprehension but I am looking for a way where I don't have to specify the list [1, 1, 2, 2, 3, 3]. Is it possible?

[j + "_" + str(i) for i, j in zip([1, 1, 2, 2, 3, 3], temp)]
like image 643
Rakesh Avatar asked Nov 01 '18 09:11

Rakesh


People also ask

How do you count unique values in a list?

In this method, we take an empty array and a count variable(set to be zero). We traverse from the start and check the items. If the item is not in the empty list(as it has taken empty) then we will add it to the empty list and increase the counter by 1.

How do you count the number of unique values in a list Python?

Using Collections to Count Unique Values in a List. The built-in collections module can be used to count unique values in a list. The module has a built-in object called Counter that returns a dictionary-like object with the unique values as keys and the number of occurrences for values.

How do you count unique ids in Python?

You can use the nunique() function to count the number of unique values in a pandas DataFrame.


2 Answers

You can use collections.defaultdict with a for loop:

from collections import defaultdict

L = ['A', 'B', 'A', 'B', 'A', 'B']

dd = defaultdict(int)

res = []
for item in L:
    dd[item] += 1
    res.append(f'{item}_{dd[item]}')

print(res)

['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']
like image 183
jpp Avatar answered Oct 20 '22 06:10

jpp


You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.

>>> from collections import Counter
>>> 
>>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
>>> seen = Counter()
>>> 
>>> result = []
>>> for c in temp:
...:    seen.update(c)
...:    result.append('{}_{}'.format(c, seen[c]))
...:    
>>> result
>>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']

Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:

>>> seen = Counter()
>>> seen.update('ABC')
>>> seen
>>> Counter({'A': 1, 'B': 1, 'C': 1})

Depending on how you want to count and what kind of data you expect, you might want to use the line

seen[c] += 1

instead of

seen.update(c)

Alternatively, without any imports:

>>> seen = {}
>>> result = []
>>> 
>>> for c in temp:
...:    seen[c] = seen.get(c, 0) + 1
...:    result.append('{}_{}'.format(c, seen[c]))
...:    
>>> result
>>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']
like image 41
timgeb Avatar answered Oct 20 '22 04:10

timgeb