Home > Net >  Get the frequency(count) of items in a nested list
Get the frequency(count) of items in a nested list

Time:10-15

I am trying to get all the frequency of all the items in numerous lists nested in a list. I really do not know how to go about it as I am relatively new to python.

The list:

Data = [['a', 'b'], ['b', 'c', 'a'], ['h', 'y', 'g'], ['a', 'c', 'y', 'b'], ['a', 'h']]

The expected output:

a = 4
b = 3
c = 2
g = 1
h = 2
y = 2

CodePudding user response:

You can use a collections.Counter for this

from collections import Counter

c = Counter()

for d in Data:
    # update the counter with the frequencies
    # of each sub-list
    c.update(d)

# get the value, count pairs from the counter
for k, v in c.items():
    # print them using a formatted string
    print(f'{k} = {v}')

a = 4
b = 3
c = 2
h = 2
y = 2
g = 1

Edit

To show how to do this with a normal dictionary

counts = {} # initialize your dictionary

for d in Data:
    for value in d:
        # using dict.get returns the value if it's there
        # or a default value if it isn't
        counts[value] = counts.get(value, 0)   1


for k, v in counts.items():
    print(f'{k} = {v}')

a = 4
b = 3
c = 2
h = 2
y = 2
g = 1

CodePudding user response:

from itertools import chain
from collections import Counter

Data = [['a', 'b'], ['b', 'c', 'a'], ['h', 'y', 'g'], ['a', 'c', 'y', 'b'], ['a', 'h']]

print(dict(Counter(chain.from_iterable(Data))))

Output:

{'a': 4, 'b': 3, 'c': 2, 'h': 2, 'y': 2, 'g': 1}

References:
chain.from_iterable
Counter

CodePudding user response:

You can also use pandas for that:

import pandas as pd
Data = [['a', 'b'], ['b', 'c', 'a'], ['h', 'y', 'g'], ['a', 'c', 'y', 'b'], ['a', 'h']]
pd.Series([d for sub in Data for d in sub]).value_counts()

output:

a    4
b    3
c    2
h    2
y    2
g    1
dtype: int64
  • Related