To count specific words in a Python string list

Asked 2 years ago, Updated 2 years ago, 49 views

"Housing houses did housing," "Is it a problem that housing houses did housing?" and "That house is a harmful house.""

We have to count the number of houses There are 9 in total

i = 0

Str = "Household houses did houses," "Household houses did houses, is there a problem?" "That house is a harmful house.""

for a in Str:

    If "housing" in a:

        i = i + 1

print(i)

Doing it like this

I counted the number of elements in the house, so only three came out.

Is there a way to count the number of specific characters?

python list

2022-09-22 08:48

2 Answers

There's nothing difficult, but if there's a quick way to get there, you can use it.

You don't have to make a wheel

But if you don't use join or count, but if you want to, I don't think you should use str.find or regular expressions. In other words, it is judged to study data structure or algorithms.

The join can be repeated and +, so it's a simple part... Count is a little bit of a big topic called search algorithm.

https://en.wikipedia.org/wiki/String-searching_algorithm

https://github.com/m00dy/string-matching-algorithms

Please refer to it and learn.


2022-09-22 08:48

Take it easy.

It's simple if you combine the strings and process them once.

Str = "Housing houses did housing," "Is it a problem that housing houses did housing?" and "That house is a harmful house.""
paragraph = ''.join(Str)
print(paragram.count('housing'))


2022-09-22 08:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.