I have a list with string
fruits = ["apple", "orange", "grape"]
and a string that's missing whitespace
str = "ilikeapplesandoranges"
What's an efficient way of counting how many words from fruits exist in str?
For the example above, it would be 2, because we have ilike[apple]sandoranges and ilikeapplesand[orange]s
For a string like ilikebroccoli, it would be 0.
I know you can do str.count(word) but that's not efficient to do for each word in the list.
It is hard to beat the C speed of
str.count(word)actually.I would use
sum(your_string.count(e) for e in your_list)and be done with it.Here is a quick benchmark of the answers here:
And that results are:
In all cases, the
str.countmethod is the fastest -- usually by orders of magnitude.