Count occurrences of a substring in a list of strings

Question:

I know that counting the simple occurrences of a list item is as easy as:

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3

But what I would like to know how to do is count every time a string appears in a substring of list entries.

For example, I want to see how many times foo appears in the list data:

data = ["the foo is all fooed", "the bar is all barred", "foo is now a bar"]

Doing:

d_count = data.count('foo')
print("d_count:", d_count)

produces:

d_count: 0

but I expect to get:

d_count: 2

I also tried doing:

d_count = data.count(any('foo' in s for s in data))
print("d_count:", d_count)

but that also gives zero as a result.

I would like to know how to count each occurrence of substring appearances in a list.

Asked By: theprowler

||

Answers:

You can try this:

from itertools import chain

data = ["the foo is all fooed", "the bar is all barred", "foo is now a bar"]

data = list(chain.from_iterable([i.split() for i in data]))

print(data.count("foo"))

Output:

2
Answered By: Ajax1234

You can do this by using the sum built-in function. No need to use list.count as well:

>>> data = ["the foo is all fooed", "the bar is all barred", "foo is now a bar"]
>>> sum('foo' in s for s in data)
2
>>>

This code works because booleans can be treated as integers. Each time 'foo' appears in a string element, True is returned. the integer value of True is 1. So it’s as if each time 'foo' is in a string, we return 1. Thus, summing the 1‘s returned will yield the number of times 1 appeared in an element.

A perhaps more explicit but equivalent way to write the above code would be:

>>> sum(1 for s in data if 'foo' in s)
2
>>> 
Answered By: Christian Dean

If data = [“abababa in foo”, “abababa”]

find occurance of “aba” from the list,
you should use below code:

>>> data = ["abababa in foo", "abababa"]
>>> str = "aba"
>>> length = len(str)
>>> sum(element[index:index+length] == str for element in data for index,char in enumerate(element))
6
Answered By: Nirmi

@ChristianDean answer was great, however it is hard to read (at least for me). So here is a more readable/easier to understand version.

count = 0
for s in data:
    if 'foo' in s:
        count += 1
Answered By: Urban P.

Counting all the ‘foo’ occurrences (not only one per string) can be done with

sum(s.count('foo') for s in data)
Answered By: Adinath Gore
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.