python - break inside list comprehension -
i have datalist , filterlist , want use list comprehension method search item in datalist string contains word filterlist:
>>> datalist=['mytest123orange','dark angle','double69','spartan','broken image 999','2 cup of tea'] >>> filterlist=['test','2','x','123','orange'] >>> print [i in datalist if len([ j j in filterlist if j in i])>0 ] ['test123orange', '2 cup of tea'] it's working want. problem value len([ j j in filterlist if j in ])>0, need loop item inside filterlist. if match first item in filterlist, loop have go through till end. example when try check 'mytest123orannge', if test in filterlist match it's enough, want 'break' loop don't want loop rest. don't need match 'orange' or '2' or '123'.
my questions :
- how can break inside loop?
- is there other better method?
use any() generator
filterlist=['test','2','x','123','orange'] print ([i in datalist if any(j j in filterlist if j in i) ]) any stops iterations when first element found
Comments
Post a Comment