Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of a substring in a list of strings

Tags:

python

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.

like image 415
theprowler Avatar asked Aug 17 '17 15:08

theprowler


People also ask

How do you count substrings in a list?

Method #2 : Using filter() + lambda + len() This function can also perform this task of finding the strings with the help of lambda. It just filters out all the strings matching the particular substring and then adds it in a new list. The len() is used to compute length of list.

How do you count occurrences of substring in a string in Python?

Python String count() Method Python String count() function is an inbuilt function in python programming language that returns the number of occurrences of a substring in the given string. Parameters: The count() function has one compulsory and two optional parameters.

How do you count occurrences in a list?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.


2 Answers

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
>>> 
like image 139
Christian Dean Avatar answered Sep 28 '22 03:09

Christian Dean


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
like image 39
Ajax1234 Avatar answered Sep 28 '22 04:09

Ajax1234