-2

I have this list

['OE21-k', 'OE28-k', 'OE21-k', 'OE-31-k', 'OE21-k', 'OE21-k', 'OE28-k']

And i want to know, how many OE21-k and how many OE28-k have. Is there any way to sum and calc how many duplicates are there?

kreiter200
  • 11
  • 2

3 Answers3

1

Yes, the collections.Counter is easy to use:

from collections import Counter

items = ['OE21-k', 'OE28-k', 'OE21-k', 'OE-31-k', 'OE21-k', 'OE21-k', 'OE28-k']
c = Counter(items)
print(c)

Output:

Counter({'OE21-k': 4, 'OE28-k': 2, 'OE-31-k': 1})
quamrana
  • 37,849
  • 12
  • 53
  • 71
1

If you want to count the occurrences just use the count() method.

['OE21-k', 'OE28-k', 'OE21-k', 'OE-31-k', 'OE21-k', 'OE21-k', 'OE28-k'].count('OE21-k')

e.g.

occurences = {}
l = ['OE21-k', 'OE28-k', 'OE21-k', 'OE-31-k', 'OE21-k', 'OE21-k', 'OE28-k'] 
for item in l:
    if item not in occurences.keys():
        occurences[item] = l.count(item)
xaostheory
  • 81
  • 5
0
occurences = {}

l = ['OE21-k', 'OE28-k', 'OE21-k', 'OE-31-k', 'OE21-k', 'OE21-k', 'OE28-k']

for item in l:
    if item in occurences:
        occurences[item] += 1
    else:
        occurences[item] = 1

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • 2
    Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem. – Sven Eberth Jul 02 '21 at 22:21