list comprehention
Hi, 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) Any suggestions or comments? Thanks. M. #my failing effort: sum([min(r.count(n)-l[:i].count(n),l.count(n)) for i,n in enumerate(l)]) #test lists import random #reference list r=[random.randint(1,5) for n in range(5)] #list l=[random.randint(1,5) for n in range(5)] -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehention
Op 19 jan 2006 vond "[EMAIL PROTECTED]" : > another approach: > > ref = [2,2,4,1,1] > lis = [2,2,5,2,4] > > len([ref.pop(ref.index(x)) for x in lis if x in ref]) > This is the type of solution I was hoping to see: one-liners, with no use of local variables. As Tim Chase already wrote, it has only one less elegant side: it alters the original ref list. Thanks for your suggestion. -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehention
Op 19 jan 2006 vond Peter Otten <[EMAIL PROTECTED]> : > sum(min(list.count(n), ref.count(n)) for n in set(ref)) > > Is that it? Seems like this is it! Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehention
Op 19 jan 2006 vond "Paddy" <[EMAIL PROTECTED]>: answer = [ val for val in set(ref) for x in range(min(lst.count(val), ref.count(val)))] answer > [2, 2, 4] I don't think it's correct. Your algoritm with the ref and lst below gives 3 as answer. The answer should have been 2 (1,3). ref=[3, 3, 1, 1, 3] lst=[5, 1, 4, 5, 3] -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehention
Op 20 jan 2006 vond Duncan Booth <[EMAIL PROTECTED]>: > Or in other words, define a function to return a dictionary containing > a count of the number of occurrences of each element in the list (this > assumes that the list elements are hashable). Then you just add up the > values in the test list making sure each count is limited to no higher > than the reference count. Thanks. Though I don't know much about python (yet), this is more or less the way I'de do it the language I'm more comfortable with (object pascal), and I wouldn't label this as a pythonic solution. I could be wrong, though:) -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehention
23 jan 2006 ta (Steven D'Aprano) shuo le: >> This is the type of solution I was hoping to see: one-liners, with no >> use of local variables. > > Because you like unreadable, incomprehensible, unmaintainable code? For practical use: no! But I'm just learning python and to understand sets/lists/dicts and it's functions, this seemed like good practise:) and besides that: it's fun! A solution with local vaiables, if-statemenst and a for loop wouldn't be much different from what I know with my current programming skills. > *wink* Yeah, yeah, I know, but I felt a response was on its place. :) -- http://mail.python.org/mailman/listinfo/python-list