Eggplant
Eggplant

Reputation: 37

Count the amount of lists in a list (nested list) for each length Python

I have a list of lists which contains strings. Like the following:

[['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple']]

There are about 2000 lists in this list, all containing a different amount of strings. What I would like to see is how many sublists of certain lengths are in this list. Like the following:

Length 2 strings : 70 lists length 3 strings: 45 lists Etcetera.

A logic way to do this (I think) is to make a loop for a length of desire, and then play this loop for all the lengths that I want the amount of lists of.

I would imagine it being something like this:

def countList(lst, x): 
    count = 0
    for i in range(len(lst)): 
        if x in lst[i]: 
            count+= 1

    return count

x = .....

But I am not sure, because I don't know how to let it count the amount.

If someone could please help me it would be great!!

Upvotes: 1

Views: 1188

Answers (4)

Mark
Mark

Reputation: 92440

You can pass the lengths to collections.Counter with something like:

from collections import Counter

l = [['apple', 'pear', 'apple'], ['apple', 'apple'],['apple', 'apple'],['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'], ['apple', 'pear', 'apple']]

counts = Counter(map(len,l))

And get dictionary counts like:

Counter({3: 2, 2: 3, 6: 1})

There are 2 of length 3, 3 of length 2, and 1 of length 6.

You can access the counts like any dictionary:

>> counts[2]
3 

Upvotes: 4

Abhishek Prusty
Abhishek Prusty

Reputation: 864

lst = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'], ['mango', 'apple'], ['mango', 'mango']]

ctr_dict = {}

for i in lst:
    item_length = len(i)
    if str(item_length) in ctr_dict:
        ctr_dict[str(item_length)] = ctr_dict[str(item_length)] + 1
    else:
        ctr_dict[str(item_length)] = 1

for k,v in ctr_dict.items():
    print(k," strings:", v, "lists")
output: 
3  strings: 1 lists
2  strings: 3 lists
6  strings: 1 lists

Upvotes: 0

vish0709
vish0709

Reputation: 38

lst = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple'],['apple', 'apple']]
def countList(lst): 
    lst_dict = {}
    for i in lst:
        if len(i) not in lst_dict:
            lst_dict[len(i)] = 1
        else:
            lst_dict[len(i)] = lst_dict[len(i)]+1
    return lst_dict

print(countList(lst))

>> {3: 1, 2: 2, 6: 1}

Here keys are the length of the lists and values are the number of lists.

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61527

The built-in collections.Counter handles this elegantly:

>>> from collections import Counter
>>> mydata = [['apple', 'pear', 'apple'], ['apple', 'apple'], ['apple', 'pear', 'apple','apple', 'pear', 'apple']]
>>> Counter(map(len, mydata))
Counter({3: 1, 2: 1, 6: 1})
>>> Counter(len(sublist) for sublist in mydata) # or with a generator expression
Counter({3: 1, 2: 1, 6: 1})

Upvotes: 5

Related Questions