> Python beginner here and very much enjoying it. I'm looking > for a pythonic way to find how many listmembers are also > present in a reference list. Don't count duplicates (eg. if > you already found a matching member in the ref list, you can't > use the ref member anymore). > > Example1: > ref=[2, 2, 4, 1, 1] > list=[2, 3, 4, 5, 3] > solution: 2 > > Example2: > ref=[2, 2, 4, 1, 1] > list=[2, 2, 5, 2, 4] > solution: 3 (note that only the first two 2's count, the third > 2 in the list should not be counted)
It sounds like you're looking for "set" operations: (using "ell" for clarity) >>> from sets import Set >>> a = [2,2,4,1,1] >>> b = [2,3,4,5,3] >>> setA = Set(a) >>> setB = Set(b) >>> results = setA.intersection(setB) >>> results Set([2,4]) >>> intersection = [x for x in results] >>> intersection [2,4] I'm a tad confused by the help, as it sounds like sets are supposed to be first-class citizens, but in ver2.3.5 that I'm running here (or rather "there", on a friend's box), I have to "import sets" which I didn't see mentioned in the reference manual. -one of many tims on the list tim = Set(["bald", "vegetarian", "loving husband"]) :) -- http://mail.python.org/mailman/listinfo/python-list