Johannes wrote: > Am 16.08.2011 09:44, schrieb Peter Otten: >> Johannes wrote: >> >>> what is the best way to check if a given list (lets call it l1) is >>> totally contained in a second list (l2)? >>> >>> for example: >>> l1 = [1,2], l2 = [1,2,3,4,5] -> l1 is contained in l2 >>> l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2 >>> l1 = [1,2,3], l2 = [1,3,5,7] -> l1 is not contained in l2 >>> >>> my problem is the second example, which makes it impossible to work with >>> sets insteads of lists. But something like set.issubset for lists would >>> be nice. >> >> Would [1, 2, 1] be contained in [1, 1, 2]? You have received an answer >> that assumes yes and another that assume no. >> > > yes it should be contained in the other one. I just work with sorted > lists, so i did not think about this case. > > geatz Johannes
For "short" lists l1: >>> pairs = [ ... ([1,2], [1,2,3,4,5]), ... ([1,2,2,], [1,2,3,4,5]), ... ([1,2,3], [1,3,5,7])] >>> for l1, l2 in pairs: ... contained = all(l1.count(item) <= l2.count(item) for item in set(l1)) ... print l1, ["not in", "in"][contained], l2 ... [1, 2] in [1, 2, 3, 4, 5] [1, 2, 2] not in [1, 2, 3, 4, 5] [1, 2, 3] not in [1, 3, 5, 7] -- http://mail.python.org/mailman/listinfo/python-list