Shi Mu said unto the world upon 2005-11-15 01:30: >>Hey Ben, >> >>first, as expected, the other two answers you received are better. :-) >> >>Sets are much better optimized for things like membership testing than >>are lists. I'm not competent to explain why; indeed, I keep >>overlooking them myself :-(
<snip discussion of my (Brian) first list-based code> >> >>Best, >> >>Brian vdB > > is it possible to modify the codes to compare the two lists with not > necessarily consecutive numbers? > For example, > lisA=[1,2,5,9] > lisB=[9,5,0] > compare A and b will get true because the two lists have 5 and 9 > together though they are indifferent order. > Best Regards, > Robert > Sure, but rather than repair a less than best first approach, better to do it with sets: >>> lisA=[1,2,5,9] >>> lisB=[9,5,0] >>> lisC=[9,5,0,1] >>> def intersection_has_two(sequence1, sequence2): set1, set2 = set(sequence1), set(sequence2) return len(set1.intersection(set2)) == 2 >>> intersection_has_two(lisA, lisB) True >>> intersection_has_two(lisA, lisC) False Sets make stuff easy. (I have got to remember that :-) The only reason to make that a function is the one liner gets a bit long for my taste: >>> len(set(lisA).intersection(set(lisB))) == 2 True >>> Best, Brian vdB -- http://mail.python.org/mailman/listinfo/python-list