Re: list comprehention

2006-01-25 Thread Mathijs
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

Re: list comprehention

2006-01-24 Thread Morten Vold
On 23/01/2006 18:41, Mathijs uttered: 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

Re: list comprehention

2006-01-24 Thread Peter Otten
[Mathijs] 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) [Morten Vold] May I suggest another one-liner: len(set(ref).intersection(lis)) I have no idea how this scales/performs

Re: list comprehention

2006-01-24 Thread Morten Vold
On 24/01/2006 09:46, Peter Otten uttered: len(set([2, 2, 4, 1, 1]).intersection([2, 2, 5, 2, 4])) 2 Close, but no cigar. I need more coffee! (note to self: Always read entire problem set first) -- Morten -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehention

2006-01-24 Thread Paddy
Umm, My answer is not correct, but for a different reason; it seems you need the length of my previous answer, thus: PythonWin 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32. Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) - see 'Help/About PythonWin' for

Re: list comprehention

2006-01-23 Thread Mathijs
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

Re: list comprehention

2006-01-23 Thread Mathijs
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

2006-01-23 Thread Mathijs
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,

Re: list comprehention

2006-01-23 Thread Mathijs
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

Re: list comprehention

2006-01-23 Thread Duncan Booth
Mathijs wrote: 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

Re: list comprehention

2006-01-23 Thread Peter Otten
Mathijs wrote: 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

Re: list comprehention

2006-01-23 Thread Patrick Maupin
Duncan Booth showed how to solve a problem posed by Mathijs. This is very similar to Duncan's solution, except I (ab)use setdefault on a regular basis... def occurrences(t): ... res = {} ... for item in t: ... res.setdefault(item,[0])[0] += 1 ... return res ... ref =

Re: list comprehention

2006-01-23 Thread Bryan Olson
Bryan Olson wrote: Duncan Booth wrote: Here's the way I would do it: def occurrences(it): res = {} for item in it: if item in res: res[item] += 1 else: res[item] = 1 return res I slightly prefer: def occurrences(it):

Re: list comprehention

2006-01-20 Thread Duncan Booth
Mathijs wrote: 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).

Re: list comprehention

2006-01-20 Thread Bryan Olson
Duncan Booth wrote: Here's the way I would do it: def occurrences(it): res = {} for item in it: if item in res: res[item] += 1 else: res[item] = 1 return res I slightly prefer: def occurrences(it): res = {} res[item] =

list comprehention

2006-01-19 Thread Mathijs
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,

Re: list comprehention

2006-01-19 Thread Tim Chase
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:

Re: list comprehention

2006-01-19 Thread [EMAIL PROTECTED]
def reference(alist1,alist2): counter = 0 for x in lis: if x in ref: ref.pop(ref.index(x)) counter += 1 return counter this works I think for your examples, but you should check it against them and other

Re: list comprehention

2006-01-19 Thread [EMAIL PROTECTED]
revision of previous: def reference(reflist,alist2): counter = 0 for x in alist2: if x in reflist: reflist.pop(reflist.index(x)) counter += 1 return counter --

Re: list comprehention

2006-01-19 Thread [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]) -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehention

2006-01-19 Thread bonono
Tim Chase wrote: 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

Re: list comprehention

2006-01-19 Thread Peter Otten
Mathijs wrote: 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).

Re: list comprehention

2006-01-19 Thread Peter Otten
Tim Chase wrote: 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. set and frozenset are a

Re: list comprehention

2006-01-19 Thread Tim Chase
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 [snipped] won't set remove duplicates which he wants to preserve ? My reading was that the solution shouldn't

Re: list comprehention

2006-01-19 Thread Matt
Tim Chase wrote: snip 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

Re: list comprehention

2006-01-19 Thread Paddy
Hi, I liked the twist at the end when you state that only the first two 2's count. It reminded me of my maths O'level revision where you always had to read the question thoroughly. Here is what I came up with: ref [2, 2, 4, 1, 1] lst [2, 2, 5, 2, 4] tmp = [ [val]*min(lst.count(val),