0

I have a list in python that looks like this:

data = ['12,34,212,90,1,8','901,2,1,8,44,1,1','77,32,11,230,894','78,23,45,89,12,20']

How do I count the number of occurrences for each number?

Output should be something like...

12:x
34:x
212:x

Where x is the number of times the value is found in the whole list

I've tried the following but not working as intended.

d = {}
#[ d.update( {i:d.get(i, 0)+1} ) for i in data ]
DobotJr
  • 3,929
  • 9
  • 36
  • 49
  • 4
    Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) You will have to flatten the list first. https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists – JeffUK Jan 12 '22 at 16:57
  • You do realize that your data is a list of strings? – Lone Survivr Jan 12 '22 at 16:58
  • you need to split your strings and convert them and then this question is an obvious duplicate as has been linked above – gold_cy Jan 12 '22 at 17:00

6 Answers6

0
data = ['12,34,212,90,1,8', '901,2,1,8,44,1,1', '77,32,11,230,894',
        '78,23,45,89,12,20']

d = {}
for item in data:
    for i in item.split(','):
        d[i] = d.get(i, 0) + 1
        
for i, j in d.items():
    print(f'{i}:{j}')

explanation : You first iterate over the data list, then you split the strings to get the numbers, then d[i] = d.get(i, 0) + 1 will do the counting. finally I iterated over the result dictionary and prints the output you want with f-string.

S.B
  • 13,077
  • 10
  • 22
  • 49
0

Convert the original list to a list of strings where the original strings are split.

data = ['12,34,212,90,1,8','901,2,1,8,44,1,1','77,32,11,230,894','78,23,45,89,12,20']
t = []
{t.extend(e.split(',')) for e in data}
print(t)

The resulting list would look like this:

['12', '34', '212', '90', '1', '8', '901', '2', '1', '8', '44', '1', '1', '77', '32', '11', '230', '894', '78', '23', '45', '89', '12', '20']

Then you simply use the count() method of the list. For instance:

t.count('1')

Will give you:

4
0

First, convert the list of string into a list containing just the values.

res = []
for i in data:
    res += i.split(',')

Then we can use python's Counter to count each value:

from collections import Counter

res = []
data = ['12,34,212,90,1,8','901,2,1,8,44,1,1','77,32,11,230,894','78,23,45,89,12,20']

for i in data:
    res += i.split(',')

res = Counter(res)
for key in res:
    print(f"{key} exist {res[key]} times")

Try it online!


Output:

12 exist 2 times
34 exist 1 times
212 exist 1 times
90 exist 1 times
1 exist 4 times
8 exist 2 times
901 exist 1 times
2 exist 1 times
44 exist 1 times
77 exist 1 times
32 exist 1 times
11 exist 1 times
230 exist 1 times
894 exist 1 times
78 exist 1 times
23 exist 1 times
45 exist 1 times
89 exist 1 times
20 exist 1 times
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

using Counter and join

# your code goes here
data = ['12,34,212,90,1,8','901,2,1,8,44,1,1','77,32,11,230,894','78,23,45,89,12,20']
from collections import Counter
result = Counter(map(int, ','.join(data).split(',')))
print(result)

# output

# Counter({1: 4, 12: 2, 8: 2, 34: 1, 212: 1, 90: 1, 901: 1, 2: 1, 44: 1, 77: 1, 32: 1, 11: 1, 230: 1, 894: 1, 78: 1, 23: 1, 45: 1, 89: 1, 20: 1})
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

Let's break down in two steps.

1. Retrieve the numbers

You have a list of strings and each string has numbers separated by commas that need to be parsed.

>>> numbers = [int(x) for s in data for x in s.split(',')]
>>> numbers
[12, 34, 212, 90, 1, 8, 901, 2, 1, 8, 44, 1, 1, 77, 32, 11, 230, 894, 78, 23, 45, 89, 12, 20]

tip: the correct order of the for statements when flatting in a comprehension is the same as if you would iterate using two nested fors.

2. Count!

>>> from collections import Counter
>>> Counter(numbers)
Counter({1: 4, 12: 2, 8: 2, 34: 1, 212: 1, 90: 1, 901: 1, 2: 1, 44: 1, 77: 1, 32: 1, 11: 1, 230: 1, 894: 1, 78: 1, 23: 1, 45: 1, 89: 1, 20: 1})
-1
d = dict()
for i in data:
    temp = i.split(",")
    for num in temp:
        n = int(num)
        if n not in d: 
            d[n] = 1
        else: 
            d[n] += 1