Re: [Tutor] Yet another list comprehension question

2007-03-06 Thread Kent Johnson
Kent Johnson wrote: > This is a popular question. It comes up frequently on comp.lang.python > and there are many recipes in the online cookbook. Here is a good starting point if you want to see a variety of ways to uniquify (?) sequences: http://tinyurl.com/3cqnj5 Make sure you look at the ref

Re: [Tutor] Yet another list comprehension question

2007-03-03 Thread Kent Johnson
Andrei wrote: >>> Alternatively, you could put the results as keys in a dictionary, >>> then request >>> mydict.keys() to get a list of unique outcomes. >> I thought of that too, but couldn't think how to do it in a list >> comprehension. It seemed like it should be possible but I >> couldn't thin

Re: [Tutor] Yet another list comprehension question

2007-03-03 Thread Andrei
>> Alternatively, you could put the results as keys in a dictionary, >> then request >> mydict.keys() to get a list of unique outcomes. > > I thought of that too, but couldn't think how to do it in a list > comprehension. It seemed like it should be possible but I > couldn't think of how - and di

Re: [Tutor] Yet another list comprehension question

2007-03-03 Thread Kent Johnson
Alan Gauld wrote: > "Smith, Jeff" <[EMAIL PROTECTED]> wrote > >> In other words, applying somefun to the results of the iterator >> return >> duplicates but I want the constructed list to contain none. > >> l = [somefun(i) for i some-iterator if somefun(i) not in l] >> >> doesn't work (not that

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread Alan Gauld
"Andrei" <[EMAIL PROTECTED]> wrote > Alternatively, you could put the results as keys in a dictionary, > then request > mydict.keys() to get a list of unique outcomes. I thought of that too, but couldn't think how to do it in a list comprehension. It seemed like it should be possible but I coul

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread Tim Golden
Andrei wrote: >> "Smith, Jeff" medplus.com> wrote >> >>> In other words, applying somefun to the results of the iterator >>> return >>> duplicates but I want the constructed list to contain none. >>> l = [somefun(i) for i some-iterator if somefun(i) not in l] >>> >>> doesn't work (not that I expe

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread David Perlman
On Mar 2, 2007, at 9:56 AM, Alan Gauld wrote: > Why not use a Set? > > s = Set([somefun(i) for i in some-iterator]) > > Might be slow for big lists though... I'm curious why using a Set would be slower than doing it in a loop? In either case, the processor has to scan through all the data l

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread Andrei
> "Smith, Jeff" medplus.com> wrote > > > In other words, applying somefun to the results of the iterator > > return > > duplicates but I want the constructed list to contain none. > > > l = [somefun(i) for i some-iterator if somefun(i) not in l] > > > > doesn't work (not that I expected it to).

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread Jordan Greenberg
Smith, Jeff wrote: > I find a common thing to do is > > l = list() > for i in some-iterator: > if somefum(i) != list: > l.append(somefun(i)) How about using the same condition you do in the if? Like: l=[somefun(i) for i in some-iterator if not type(somefun(i)) is list] HTH Jordan _

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread Alan Gauld
"Smith, Jeff" <[EMAIL PROTECTED]> wrote > In other words, applying somefun to the results of the iterator > return > duplicates but I want the constructed list to contain none. > l = [somefun(i) for i some-iterator if somefun(i) not in l] > > doesn't work (not that I expected it to). Why not u

[Tutor] Yet another list comprehension question

2007-03-02 Thread Smith, Jeff
I find a common thing to do is l = list() for i in some-iterator: if somefum(i) != list: l.append(somefun(i)) In other words, applying somefun to the results of the iterator return duplicates but I want the constructed list to contain none. l = [somefun(i) for i some-iterator] will