1

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!!

1
  • 1
    Could you please make your expected output more clear? Commented May 2, 2020 at 17:41

4 Answers 4

5

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})
Sign up to request clarification or add additional context in comments.

Comments

4

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 

Comments

0
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.

Comments

0
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

1 Comment

Happy to help, and welcome to Stack Overflow ! If this answer or any other one really solved your issue, please mark it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.