简体   繁体   中英

Count occurrences of a substring in a list of strings

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.

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

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

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

@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

I dont understand why are we not using count method for string as we already can use it. Btw I did it using following:

sum(s.count('foo') for s in data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM