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.