问题描述:
I’ve a fixed list of strings, pets = ['rabbit','parrot','dog','cat','hamster']
and I need to match another string basket = ['apple','dog','shirt']
. This basket
will be varying.
Typically pets
will have 300 element string while basket
will have 5 element string.
I need to find if ANY of the elements of basket
are from pets
and return true with first match.
I’m currently doing it like this
for item in basket:
if item in pets:
flag=1
break
There must be a faster way
解决方案 1:[1]
Since, as you said, pets
will be a fixed list and basket
will be varying, you should make pets a set so that it can be checked faster. basket
should remain a list if you are only going to check it once each time.
set_of_pets = set(pets)
//then presumably in a loop where you have a different basket each time:
found = any(item in set_of_pets for item in basket)
The idea is that you only turn pets
into a set once, which is O(N) (where N is the size of pets), and reuse it each time you check a basket. The any
operation will then be O(n), where n is the basket size, instead of O(n*N) if you kept pets
as a list.
For further optimization, this question shows that the following will be even faster:
found = any(True for item in basket if item in set_of_pets)
A future version of Python may conceivably optimize the first snippet to be as fast as the second one, so unless performance is of utmost importance, do whatever you find more readable. (And if performance is important, measure and don’t assume.)
解决方案 2:[2]
You can use sets for example…
def find_matching_pet(basket, pets):
return bool(set(basket) & set(pets))
参考链接:
Copyright Notice: This article follows StackOverflow’s copyright notice requirements and is licensed under CC BY-SA 3.0.
Article Source: StackOverflow
[1] k314159
[2] Tino D