python 2.7 - faster way to check list on membership multiple values -
i want test if multiple values have membership on list, script slow. tried far:
list1 = [10,20,35,45,67,88,99] x in list1: if 9<x<11: x in list1: if 34<x<39: x in list1: if 87<x<90: print "yeah"
the general idea finish iteration once conditions satisfied.
there might cleverer ways solve it, simplest can think of keeping track of conditions' results, , breaking true
. example:
list = [10, 20, 35, 45, 67, 88, 99, 0] // there keep track of each test cond1 = false cond2 = false cond3 = false // each of conds become true , keep value. x in list: cond1 = cond1 or (9 < x < 11) cond2 = cond2 or (34 < x < 39) cond3 = cond3 or (87 < x < 90) if cond1 , cond2 , cond3: // once of them become true, know want , can break loop print "yeah" break
wiki
Comments
Post a Comment