I have list_a
and string_tmp
like this
list_a = ['AA', 'BB', 'CC']
string_tmp = 'Hi AA How Are You'
I want to find out is there any of string_tmp
items in the list_a
, if it is, type = L1
else type = L2
?
# for example
type = ''
for k in string_tmp.split():
if k in list_a:
type = 'L1'
if len(type) == 0:
type = 'L2'
this is the real problem but in my project, len(list_a) = 200,000
and len(strgin_tmp) = 10,000
, so I need that to be super fast
# this is the output of the example
type = 'L1'
Converting the reference list and string tokens to sets should enhance performance. Something like this:
list_a = ['AA', 'BB', 'CC']
string_tmp = 'Hi AA How Are You'
def get_type(s, r): # s is the string, r is the reference list
s = set(s.split())
r = set(r)
return 'L1' if any(map(lambda x: x in r, s)) else 'L2'
print(get_type(string_tmp, list_a))
Output:
L1