Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Wolfram Hinderer
' '.join(x for x in l)", 'l = list(map(str,range(10)))').timeit() 3.7431774848480472 Much smaller factor now. But wait! If we want to test list comprehension against generator comprehension, we should try a function that just consumes the iterable. >>> setup = &qu

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Interestingly, I scaled it up to a million list items with more or less the same results. It's helpful to see that your results are different. That leads me to suspect that mine are somehow related to my own environment. Still I think the key is the overhead in calling next() for each item in th

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Stephen Hansen
On Tue, Jan 19, 2010 at 7:30 AM, Gerald Britton wrote: [snip] > mystring = '\n'.join( line for line in lines if depending on line> ) > Note, this is not a list comprehension, but a generator comprehension. A list comprehension is used to build, in one sweep, a list and return it. A generator c

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Thanks! Good explanation. On Tue, Jan 19, 2010 at 10:57 AM, Alf P. Steinbach wrote: > * Gerald Britton: >> >> Yesterday I stumbled across some old code in a project I was working >> on.  It does something like this: >> >> mystring = '\n'.join( [ line for line in lines if > depending on line> ] )

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Alf P. Steinbach
* Gerald Britton: Yesterday I stumbled across some old code in a project I was working on. It does something like this: mystring = '\n'.join( [ line for line in lines if ] ) where "lines" is a simple list of strings. I realized that the code had been written before you could put a list compr

Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Yesterday I stumbled across some old code in a project I was working on. It does something like this: mystring = '\n'.join( [ line for line in lines if ] ) where "lines" is a simple list of strings. I realized that the code had been written before you could put a list comprehension as an argum

Re: Partial list comprehensions

2009-12-04 Thread Stephen Hansen
On Fri, Dec 4, 2009 at 2:11 PM, Joel Davis wrote: > Yes, sort of like that but Hansen's code is actually exactly what I > was getting at, not sure why he deleted it: Huh? I deleted it? --S -- http://mail.python.org/mailman/listinfo/python-list

Re: Partial list comprehensions

2009-12-04 Thread Joel Davis
On Dec 4, 3:41 pm, Mensanator wrote: > On Dec 4, 2:22 pm, Joel Davis wrote: > > > Is it possible to run a list comprehension over a certain portion of > > the list? My goals is to be able to run the comprehension on the > > innermost elements of the list, but leaving the outermost intact. > > Som

Re: Partial list comprehensions

2009-12-04 Thread Mensanator
On Dec 4, 2:22 pm, Joel Davis wrote: > Is it possible to run a list comprehension over a certain portion of > the list? My goals is to be able to run the comprehension on the > innermost elements of the list, but leaving the outermost intact. Something like this? >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8

Re: Partial list comprehensions

2009-12-04 Thread Stephen Hansen
> > Is it possible to run a list comprehension over a certain portion of > the list? My goals is to be able to run the comprehension on the > innermost elements of the list, but leaving the outermost intact. > Without seeing an example of what your list is, and what you want to turn it into, its s

Partial list comprehensions

2009-12-04 Thread Joel Davis
Is it possible to run a list comprehension over a certain portion of the list? My goals is to be able to run the comprehension on the innermost elements of the list, but leaving the outermost intact. -- http://mail.python.org/mailman/listinfo/python-list

Python 3 [was Re: substituting list comprehensions for map()]

2009-11-04 Thread Steven D'Aprano
On Wed, 04 Nov 2009 23:08:54 +1100, Ben Finney wrote: > Steven D'Aprano writes: > >> On Tue, 03 Nov 2009 22:43:45 -0600, Robert Kern wrote: >> > from numpy import dot >> > >> > scalar = dot(vec1, vec2) >> >> Why would I want to use an already existing library that is fast, well- >> written and

Re: substituting list comprehensions for map()

2009-11-04 Thread J Kenneth King
d use map, it could use a for- > loop, a while loop, recursion, or black magic: > > scalar = dot_product(vec1, vec2) Even better. But now I'm afraid the example is running away from the point. So to summarize: 1. Extra name bindings and loop keywords aren't always easier to re

Re: substituting list comprehensions for map()

2009-11-04 Thread Ben Finney
Steven D'Aprano writes: > On Tue, 03 Nov 2009 22:43:45 -0600, Robert Kern wrote: > > from numpy import dot > > > > scalar = dot(vec1, vec2) > > Why would I want to use an already existing library that is fast, > well- written and well-supported, when I can toss together a nasty > kludge myself?

Re: substituting list comprehensions for map()

2009-11-04 Thread Tim Chase
Steven D'Aprano wrote: On Tue, 03 Nov 2009 22:43:45 -0600, Robert Kern wrote: Or use the appropriate libraries: from numpy import dot scalar = dot(vec1, vec2) Why would I want to use an already existing library that is fast, well- written and well-supported, when I can toss together a nasty

Re: substituting list comprehensions for map()

2009-11-03 Thread Steven D'Aprano
On Tue, 03 Nov 2009 22:43:45 -0600, Robert Kern wrote: > Steven D'Aprano wrote: >> On Tue, 03 Nov 2009 10:22:28 -0500, J Kenneth King wrote: > >>> Adding in the loop construct and name bindings doesn't enhance my >>> understanding of what a dot-product is. I don't need to see the loop >>> constr

Re: substituting list comprehensions for map()

2009-11-03 Thread Robert Kern
Steven D'Aprano wrote: On Tue, 03 Nov 2009 10:22:28 -0500, J Kenneth King wrote: Adding in the loop construct and name bindings doesn't enhance my understanding of what a dot-product is. I don't need to see the loop construct at all in this case. A dot product is simply the multiplication of

Re: substituting list comprehensions for map()

2009-11-03 Thread Steven D'Aprano
On Tue, 03 Nov 2009 10:22:28 -0500, J Kenneth King wrote: > However in this case the procedure by which we derive the value is not > important or even interesting. It is much more succinct to think of the > operation as a value and express it accordingly. There's no need to > clutter the mind wi

Re: substituting list comprehensions for map()

2009-11-03 Thread J Kenneth King
en it doesn't solve the stated > problem: to replace use of ‘map()’ with a list comprehension. Granted I admit this later in my post. It's not a solution to the OPs request, but it is the best solution to such a case. > >> I understand the OP was asking for it, but list compreh

Re: substituting list comprehensions for map()

2009-11-03 Thread Anh Hai Trinh
> Yes, just about any ‘map()’ operation has a corresponding list > comprehension. (Does anyone know of a counter-example, a ‘map()’ > operation that doesn't have a correspondingly simple list > comprehension?) Try turning this into a list comprehension: vectorsum = lambda *args: map(sum, zip(*a

Re: substituting list comprehensions for map()

2009-11-02 Thread Sean DiZazzo
On Nov 2, 9:01 pm, Ben Finney wrote: > Anh Hai Trinh writes: > > > > Yes, just about any ‘map()’ operation has a corresponding list > > > comprehension. (Does anyone know of a counter-example, a ‘map()’ > > > operation that doesn't have a correspondingly simple list > > > comprehension?) > > > Tr

Re: substituting list comprehensions for map()

2009-11-02 Thread Steven D'Aprano
On Mon, 02 Nov 2009 20:06:51 -0800, Anh Hai Trinh wrote: >> Yes, just about any ‘map()’ operation has a corresponding list >> comprehension. (Does anyone know of a counter-example, a ‘map()’ >> operation that doesn't have a correspondingly simple list >> comprehension?) > > Try turning this into

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Anh Hai Trinh writes: > > Yes, just about any ‘map()’ operation has a corresponding list > > comprehension. (Does anyone know of a counter-example, a ‘map()’ > > operation that doesn't have a correspondingly simple list > > comprehension?) > > Try turning this into a list comprehension: > > vec

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Steven D'Aprano writes: > On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote: > > > "Jon P." writes: > > > >> I'd like to do: > >> > >> resultlist = operandlist1 + operandlist2 > > > > That's an unfortunate way of expressing it; it's valid Python syntax > > that doesn't do what you're descri

Re: substituting list comprehensions for map()

2009-11-02 Thread Anh Hai Trinh
> Try turning this into a list comprehension: > >   vectorsum = lambda *args: map(sum, zip(*args)) > >   vectorsum([1,2], [3,4], [5,6]) > ->[9, 12] >   vectorsum([1,2], [3,4], [5,6], [7,8]) > ->[16, 20] Nvm, it's actually easy: vectorsum = lambda *args: [sum(i) for i in zip(*args)] -- http://ma

Re: substituting list comprehensions for map()

2009-11-02 Thread Anh Hai Trinh
> On the other hand, list comps using an if clause can't be written as pure > maps. You can do this: > > [func(x) for x in seq if cond(x)] > > filter(cond, map(func, seq)) > > but the second version may use much more temporary memory if seq is huge > and cond very rarely true. You could use ifilte

Re: substituting list comprehensions for map()

2009-11-02 Thread Steven D'Aprano
On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote: > "Jon P." writes: > >> I'd like to do: >> >> resultlist = operandlist1 + operandlist2 > > That's an unfortunate way of expressing it; it's valid Python syntax > that doesn't do what you're describing (in this case, it will bind > ‘resultlis

Re: substituting list comprehensions for map()

2009-11-02 Thread Steven D'Aprano
ge to say it's a solution, when it doesn't solve the stated > problem: to replace use of ‘map()’ with a list comprehension. In context, I wasn't giving that as a replacement for map(), but as a replacement for map-with-a-lambda. >> I understand the OP was asking for it, b

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
‘map()’ with a list comprehension. > I understand the OP was asking for it, but list comprehensions aren't > the best solution in this case... it would just be line noise. I disagree; a list comprehension is often clearer than use of ‘map()’ with a lambda form, and I find it to be so in thi

Re: substituting list comprehensions for map()

2009-11-02 Thread J Kenneth King
dlist1, operandlist2) This is the best solution so far. > > >> Is there any reasonable way to do this via a list comprehension ? > > [x+y for (x, y) in zip(operandlist1, operandlist2)] > > If the lists are huge, you can save some temporary memory by replacing > zip wit

Re: substituting list comprehensions for map()

2009-11-02 Thread Bruno Desthuilliers
Ben Finney a écrit : Bruno Desthuilliers writes: Ben Finney a écrit : (Does anyone know of a counter-example, a ‘map()’ operation that doesn't have a correspondingly simple list comprehension?) ... depends on your definition of "simple". There are things I'd rather not write as a list compre

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Bruno Desthuilliers writes: > Ben Finney a écrit : > > (Does anyone know of a counter-example, a ‘map()’ operation that > > doesn't have a correspondingly simple list comprehension?) > > ... depends on your definition of "simple". There are things I'd > rather not write as a list comprehension...

Re: substituting list comprehensions for map()

2009-11-02 Thread Diez B. Roggisch
Steven D'Aprano schrieb: On Sun, 01 Nov 2009 23:54:16 -0800, Jon P. wrote: I'd like to do: resultlist = operandlist1 + operandlist2 where for example operandlist1=[1,2,3,4,5] operandlist2=[5,4,3,2,1] and resultlist will become [6,6,6,6,6]. Using map(), I can do: map(lambda op1,op2: op1 +

Re: substituting list comprehensions for map()

2009-11-02 Thread Neil Crighton
Steven D'Aprano REMOVE.THIS.cybersource.com.au> writes: > > > > operandlist1=[1,2,3,4,5] > > operandlist2=[5,4,3,2,1] > > > > and resultlist will become [6,6,6,6,6]. Using map(), I can do: > > > > map(lambda op1,op2: op1 + op2, operandlist1, operandlist2) > > If the two lists are very large,

Re: substituting list comprehensions for map()

2009-11-02 Thread Bruno Desthuilliers
Ben Finney a écrit : (snip) Yes, just about any ‘map()’ operation has a corresponding list comprehension. Right AFAICT, but: (Does anyone know of a counter-example, a ‘map()’ operation that doesn't have a correspondingly simple list comprehension?) ... depends on your definition of "simple"

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
"Jon P." writes: > I'd like to do: > > resultlist = operandlist1 + operandlist2 That's an unfortunate way of expressing it; it's valid Python syntax that doesn't do what you're describing (in this case, it will bind ‘resultlist’ to a new list that is the *concatenation* of the two original lists

Re: substituting list comprehensions for map()

2009-11-02 Thread Paul Rudin
"Jon P." writes: > I'd like to do: > > resultlist = operandlist1 + operandlist2 > > where for example > > operandlist1=[1,2,3,4,5] > operandlist2=[5,4,3,2,1] > > and resultlist will become [6,6,6,6,6]. Using map(), I > can do: > > map(lambda op1,op2: op1 + op2, operandlist1, operandlist2) > > Is

Re: substituting list comprehensions for map()

2009-11-02 Thread Steven D'Aprano
On Sun, 01 Nov 2009 23:54:16 -0800, Jon P. wrote: > I'd like to do: > > resultlist = operandlist1 + operandlist2 > > where for example > > operandlist1=[1,2,3,4,5] > operandlist2=[5,4,3,2,1] > > and resultlist will become [6,6,6,6,6]. Using map(), I can do: > > map(lambda op1,op2: op1 + op2,

Re: substituting list comprehensions for map()

2009-11-02 Thread Chris Rebert
On Mon, Nov 2, 2009 at 12:54 AM, Jon P. wrote: > I'd like to do: > > resultlist = operandlist1 + operandlist2 > > where for example > > operandlist1=[1,2,3,4,5] > operandlist2=[5,4,3,2,1] > > and resultlist will become [6,6,6,6,6].  Using map(), I > can do: > > map(lambda op1,op2: op1 + op2, opera

Re: substituting list comprehensions for map()

2009-11-02 Thread Javier Collado
Hello, I'll do the following: [op1+op2 for op1,op2 in zip(operandlist1, operandlist2)] Best regards, Javier 2009/11/2 Jon P. : > I'd like to do: > > resultlist = operandlist1 + operandlist2 > > where for example > > operandlist1=[1,2,3,4,5] > operandlist2=[5,4,3,2,1] > > and resultlist will

substituting list comprehensions for map()

2009-11-01 Thread Jon P.
I'd like to do: resultlist = operandlist1 + operandlist2 where for example operandlist1=[1,2,3,4,5] operandlist2=[5,4,3,2,1] and resultlist will become [6,6,6,6,6]. Using map(), I can do: map(lambda op1,op2: op1 + op2, operandlist1, operandlist2) Is there any reasonable way to do this via a

Re: question regarding list comprehensions

2008-10-20 Thread Pat
Diez B. Roggisch wrote: Pat wrote: I have written chunks of Python code that look this: new_array = [] for a in array: if not len( a ): continue new_array.append( a ) new_array = [a for a in array if len(a)] and... string = "" for r in r

Re: question regarding list comprehensions

2008-10-20 Thread Pat
Steven D'Aprano wrote: On Mon, 20 Oct 2008 10:20:03 -0400, Pat wrote: Finally, if someone could point me to a good tutorial or explain list compressions I would be forever in your debt. Think of a for-loop: for x in (1, 2, 3): x Creates x=1, then x=2, then x=3. It doesn't do anything wi

Re: question regarding list comprehensions

2008-10-20 Thread Bruno Desthuilliers
#x27;#')) It seems that a list comprehension could clean up the code, but I seem to have a mental block on list comprehensions. I've read up a lot on this subject in my books and on the Internet and for whatever reason, I'm having problems with this idiom (if that's the cor

Re: question regarding list comprehensions

2008-10-20 Thread Steven D'Aprano
On Mon, 20 Oct 2008 10:20:03 -0400, Pat wrote: > Finally, if someone could point me to a good tutorial or explain list > compressions I would be forever in your debt. Think of a for-loop: for x in (1, 2, 3): x Creates x=1, then x=2, then x=3. It doesn't do anything with the x's, but just c

Re: question regarding list comprehensions

2008-10-20 Thread Diez B. Roggisch
Pat wrote: > I have written chunks of Python code that look this: > > new_array = [] > for a in array: > if not len( a ): > continue > new_array.append( a ) new_array = [a for a in array if len(a)] > and... > > string = "" > for r in results:

question regarding list comprehensions

2008-10-20 Thread Pat
It seems that a list comprehension could clean up the code, but I seem to have a mental block on list comprehensions. I've read up a lot on this subject in my books and on the Internet and for whatever reason, I'm having problems with this idiom (if that's the correct expression

Re: Newbie question about tuples and list comprehensions

2008-06-26 Thread Peter Otten
idiolect wrote: > On Jun 25, 7:26 pm, Terry Reedy <[EMAIL PROTECTED]> wrote: >> idiolect wrote: >> > Hi all - Sorry to plague you with another newbie question from a >> > lurker. Hopefully, this will be simple. >> >> > I have a list full of RGB pixel values read from an image. I want to >> > tes

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread William McBrine
On Wed, 25 Jun 2008 16:02:52 -0700, John Machin wrote: > Here's one approach (requires Python 2.5 or later): > [(50 if x < 50 else x, 50 if y < 50 else y, 50 if z < 50 else z) for > (x, y, z) in source] [(max(x, 50), max(y, 50), max(z, 50)) for (x, y, z) in source] -- 09 F9 11 02 9D 74 E3 5B D8

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread idiolect
On Jun 25, 7:26 pm, Terry Reedy <[EMAIL PROTECTED]> wrote: > idiolect wrote: > > Hi all - Sorry to plague you with another newbie question from a > > lurker. Hopefully, this will be simple. > > > I have a list full of RGB pixel values read from an image. I want to > > test each RGB band value per

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread Terry Reedy
idiolect wrote: Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band value per pixel, and set it to something else if it meets or falls below a certain

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread Roger Miller
First, I second Matt's comment about using a boring old for loop when it is the simplest way to express what you want to do. Being Pythonic is not about using exotic features to scrunch your code down to a cool one-liner. It is about expressing your intentions in a simple, direct way. Sometimes

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread John Machin
in > general is rather naive, so I would appreciate any insight anyone can > offer since I am not sure if I'm even headed down the correct path > with list comprehensions. > "x or y or z < 50" doesn't do what you think it does: >>> x, y, z = 120, 130, 140 &

Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread Matimus
in > general is rather naive, so I would appreciate any insight anyone can > offer since I am not sure if I'm even headed down the correct path > with list comprehensions. > > Much obliged! List comprehension is just a tool. If it looks more complicated than it would if you used

Newbie question about tuples and list comprehensions

2008-06-25 Thread idiolect
really test and change each value in the tuple individually. My understanding of the things you can do with lists and python in general is rather naive, so I would appreciate any insight anyone can offer since I am not sure if I'm even headed down the correct path with list comprehensions. Much o

Re: Misuse of list comprehensions?

2008-06-03 Thread Lie
On May 20, 8:51 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > John Salerno: > >> What does everyone think about this? > > > The Example 2 builds a list, that is then thrown away. It's just a > > waste of memory (and time). > > No, it doesn't. It uses append becaus

Re: Misuse of list comprehensions?

2008-05-27 Thread Ian Kelly
On Tue, May 27, 2008 at 8:08 PM, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > En Tue, 27 May 2008 14:43:52 -0300, Ian Kelly <[EMAIL PROTECTED]> > escribió: > >> It sounds like the wasteful list creation is the biggest objection to >> using a list comprehension. I'm curious what people think of t

Re: Misuse of list comprehensions?

2008-05-27 Thread Ian Kelly
On Tue, May 27, 2008 at 7:09 PM, Delaney, Timothy (Tim) <[EMAIL PROTECTED]> wrote: > Ian Kelly wrote: > >> It sounds like the wasteful list creation is the biggest objection to >> using a list comprehension. I'm curious what people think of this >> alternative, which avoids populating the list by

Re: Misuse of list comprehensions?

2008-05-27 Thread Gabriel Genellina
En Tue, 27 May 2008 14:43:52 -0300, Ian Kelly <[EMAIL PROTECTED]> escribió: It sounds like the wasteful list creation is the biggest objection to using a list comprehension. I'm curious what people think of this alternative, which avoids populating the list by using a generator expression ins

RE: Misuse of list comprehensions?

2008-05-27 Thread Delaney, Timothy (Tim)
Ian Kelly wrote: > It sounds like the wasteful list creation is the biggest objection to > using a list comprehension. I'm curious what people think of this > alternative, which avoids populating the list by using a generator > expression instead (apart from the fact that this is still quadratic,

Re: Misuse of list comprehensions?

2008-05-27 Thread MRAB
On May 27, 6:43 pm, "Ian Kelly" <[EMAIL PROTECTED]> wrote: > On Tue, May 20, 2008 at 11:19 AM, John Salerno <[EMAIL PROTECTED]> wrote: > > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message > >news:[EMAIL PROTECTED] > >> After being corrected about missing the construction of a None-containing

Re: Misuse of list comprehensions?

2008-05-27 Thread Arnaud Delobelle
"Ian Kelly" <[EMAIL PROTECTED]> writes: > On Tue, May 20, 2008 at 11:19 AM, John Salerno <[EMAIL PROTECTED]> wrote: >> "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >>> After being corrected about missing the construction of a None-containing >>> list, one needs

Re: Misuse of list comprehensions?

2008-05-27 Thread Ian Kelly
On Tue, May 20, 2008 at 11:19 AM, John Salerno <[EMAIL PROTECTED]> wrote: > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> After being corrected about missing the construction of a None-containing >> list, one needs of course to think about the waste of resource

Re: Misuse of list comprehensions?

2008-05-23 Thread duncan smith
Simon Forman wrote: On May 21, 4:36 am, Bruno Desthuilliers wrote: Simon Forman a écrit : On May 20, 8:58 am, Paul McGuire <[EMAIL PROTECTED]> wrote: On May 20, 10:50 am, [EMAIL PROTECTED] wrote: You don't need all those conditionals. A set differs from a list precisely in the fact that e

Re: Misuse of list comprehensions?

2008-05-21 Thread Simon Forman
On May 21, 4:36 am, Bruno Desthuilliers wrote: > Simon Forman a écrit : > > > > > On May 20, 8:58 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > >> On May 20, 10:50 am, [EMAIL PROTECTED] wrote: > > >>> You don't need all those conditionals. A set differs from a list > >>> precisely in the fact that

Re: Misuse of list comprehensions?

2008-05-21 Thread cokofreedom
''.join(seen.add(c) or c for c in s if c not in seen) >From what I can understand... .join will be a String of unique 'c' values. 'seen.add(c) or c' will always point to the second statement 'c' because seen.add(c) returns None. 'if c not in seen' will ensure 'c' being added to both the .join a

Re: Misuse of list comprehensions?

2008-05-21 Thread Bruno Desthuilliers
Simon Forman a écrit : On May 20, 8:58 am, Paul McGuire <[EMAIL PROTECTED]> wrote: On May 20, 10:50 am, [EMAIL PROTECTED] wrote: You don't need all those conditionals. A set differs from a list precisely in the fact that each element is unique. And since the function is expecting "s" to be a

Re: Misuse of list comprehensions?

2008-05-20 Thread Simon Forman
On May 20, 8:58 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > On May 20, 10:50 am, [EMAIL PROTECTED] wrote: > > > > > You don't need all those conditionals. A set differs from a list > > precisely in the fact that each element is unique. And since the > > function is expecting "s" to be an iterable

Re: Misuse of list comprehensions?

2008-05-20 Thread Colin J. Williams
ake an in-place change is explicit, and it's being used as everyone expects it to be used. In example 2, however, I began to think this might be an abuse of list comprehensions, because I'm not assigning the result to anything (nor am I even using the result in any way). What does eve

Re: Misuse of list comprehensions?

2008-05-20 Thread Larry Bates
ake an in-place change is explicit, and it's being used as everyone expects it to be used. In example 2, however, I began to think this might be an abuse of list comprehensions, because I'm not assigning the result to anything (nor am I even using the result in any way). What does eve

Re: Misuse of list comprehensions?

2008-05-20 Thread Colin J. Williams
1, the intention to make an in-place change is explicit, and it's being used as everyone expects it to be used. In example 2, however, I began to think this might be an abuse of list comprehensions, because I'm not assigning the result to anything (nor am I even using the result in a

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 11:08 am, Paul Hankin <[EMAIL PROTECTED]> wrote: > On May 20, 3:58 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > > > def compress(s): > >     seen = set() > >     return ''.join(c for c in s if c not in seen and (seen.add(c) or > > True)) > > Slightly nicer is to move the set add out of

Re: Misuse of list comprehensions?

2008-05-20 Thread Paddy
- > > > In example 1, the intention to make an in-place change is explicit, and it's > > being used as everyone expects it to be used. In example 2, however, I began > > to think this might be an abuse of list comprehensions, because I'm not > > assigning th

Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Thomas Bellman" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > "John Salerno" <[EMAIL PROTECTED]> writes: > >> "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >>> the above code is pretty much of a no-no because it has quadratic >>> runtime >>> be

Re: Misuse of list comprehensions?

2008-05-20 Thread s0suk3
On May 20, 12:22 pm, "John Salerno" <[EMAIL PROTECTED]> wrote: > "Paul McGuire" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > On May 20, 10:50 am, [EMAIL PROTECTED] wrote: > > > def compress(s): > > return list(set(s)) > > > That does the trick. > > Wow, I just *knew* there had

Re: Misuse of list comprehensions?

2008-05-20 Thread Thomas Bellman
"John Salerno" <[EMAIL PROTECTED]> writes: > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> the above code is pretty much of a no-no because it has quadratic runtime >> behavior. > What's that mean, exactly? It means that running time will increase with the s

Re: Misuse of list comprehensions?

2008-05-20 Thread Terry Reedy
"John Salerno" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message | news:[EMAIL PROTECTED] | > the above code is pretty much of a no-no because it has quadratic runtime | > behavior. | | | What's that mean, exactly? Are you refer

Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Bruno Desthuilliers" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> def compress(s): >> new = [] >> [new.append(c) for c in s if c not in new] >> return ''.join(new) > > As far as I'm concerned (and I'm a big fan of list-comps, generator > expressions etc), this is d

Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] On May 20, 10:50 am, [EMAIL PROTECTED] wrote: > def compress(s): > return list(set(s)) > > That does the trick. Wow, I just *knew* there had to be some built-in function that would make this problem trivial! :) Only if o

Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > After being corrected about missing the construction of a None-containing > list, one needs of course to think about the waste of resources, as a > possible result-list is created in any case. Yeah, I was already awa

Re: Misuse of list comprehensions?

2008-05-20 Thread Arnaud Delobelle
Paul McGuire <[EMAIL PROTECTED]> writes: > On May 20, 10:17 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] >> >> Isn't >> >>     c not in seen and (seen.add(c) or True) >> >> the same as >> >>     seen.add(c) or c not in seen >> >> ? >> >> >     return ''.join(new) >> >> (notice I haven't

Re: Misuse of list comprehensions?

2008-05-20 Thread John Salerno
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > the above code is pretty much of a no-no because it has quadratic runtime > behavior. What's that mean, exactly? Are you referring to both examples, or just the second one? -- http://mail.python.org/mailman/list

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul Hankin
On May 20, 3:58 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > def compress(s): > seen = set() > return ''.join(c for c in s if c not in seen and (seen.add(c) or > True)) Slightly nicer is to move the set add out of the conditional... def compress(s): seen = set() return ''.join(s

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 10:50 am, [EMAIL PROTECTED] wrote: > > You don't need all those conditionals. A set differs from a list > precisely in the fact that each element is unique. And since the > function is expecting "s" to be an iterable object, it can be > constructed even without a for loop: > > def compre

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
t;>     [new.append(c) for c in s if c not in new] > >>     return ''.join(new) > >> -- > > >> In example 1, the intention to make an in-place change is explicit, and > >> it's > >> being used as eve

Re: Misuse of list comprehensions?

2008-05-20 Thread s0suk3
- > > > In example 1, the intention to make an in-place change is explicit, and it's > > being used as everyone expects it to be used. In example 2, however, I began > > to think this might be an abuse of list comprehensions, because I'm not > > assigning th

Re: Misuse of list comprehensions?

2008-05-20 Thread Arnaud Delobelle
ample 1, the intention to make an in-place change is explicit, and it's >> being used as everyone expects it to be used. In example 2, however, I began >> to think this might be an abuse of list comprehensions, because I'm not >> assigning the result to anything (nor am I even usi

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
;     new = [] >     [new.append(c) for c in s if c not in new] >     return ''.join(new) > -- > > In example 1, the intention to make an in-place change is explicit, and it's > being used as everyone expects it to be used. In example 2, however, I began &g

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
Thomas Bellman wrote: > "Diez B. Roggisch" <[EMAIL PROTECTED]> writes: > >> [EMAIL PROTECTED] wrote: > >>> The Example 2 builds a list, that is then thrown away. It's just a >>> waste of memory (and time). > >> No, it doesn't. It uses append because it refers to itself in the >> if-expression.

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
> That being said, I use that idiom myself. But I don't see anything wrong > with using a list-comp as loop-abbreviation. because that is it's actual > purpose. And also it is common in non-functional languages that l-values > aren't always assigned, if the aren't needed. It's the consequence of >

Re: Misuse of list comprehensions?

2008-05-20 Thread Thomas Bellman
if-expression. So the append(c) is needed - and thus the assignment > possible but essentially useless. Yes it does. A list comprehension *always* creates a list. In this case it will be a list of None, since that is what list.append() returns. See this: >>> new

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
Hrvoje Niksic wrote: > "Diez B. Roggisch" <[EMAIL PROTECTED]> writes: > >> [EMAIL PROTECTED] wrote: >> >>> John Salerno: What does everyone think about this? >>> >>> The Example 2 builds a list, that is then thrown away. It's just a >>> waste of memory (and time). >> >> No, it doesn't. > >

Re: Misuse of list comprehensions?

2008-05-20 Thread Hrvoje Niksic
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: > [EMAIL PROTECTED] wrote: > >> John Salerno: >>> What does everyone think about this? >> >> The Example 2 builds a list, that is then thrown away. It's just a >> waste of memory (and time). > > No, it doesn't. This line: [new.append(c) for c i

Re: Misuse of list comprehensions?

2008-05-20 Thread Bruno Desthuilliers
John Salerno a écrit : I posted this code last night in response to another thread, and after I posted it I got to wondering if I had misused the list comprehension. (snip) def compress(s): new = [] [new.append(c) for c in s if c not in new] return ''.join(new) As far as I'm con

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote: > John Salerno: >> What does everyone think about this? > > The Example 2 builds a list, that is then thrown away. It's just a > waste of memory (and time). No, it doesn't. It uses append because it refers to itself in the if-expression. So the append(c) is needed - and

Re: Misuse of list comprehensions?

2008-05-20 Thread bearophileHUGS
John Salerno: > What does everyone think about this? The Example 2 builds a list, that is then thrown away. It's just a waste of memory (and time). Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
t; return ''.join(new) > -- > > In example 1, the intention to make an in-place change is explicit, and > it's being used as everyone expects it to be used. In example 2, however, > I began to think this might be an abuse of list com

Misuse of list comprehensions?

2008-05-20 Thread John Salerno
explicit, and it's being used as everyone expects it to be used. In example 2, however, I began to think this might be an abuse of list comprehensions, because I'm not assigning the result to anything (nor am I even using the result in any way). What does everyone think about this?

Re: variable scope in list comprehensions

2008-04-05 Thread Steve Holden
>> >>> Does anybody understand it? >>> >>> >> This isn't _a_ list comprehension, it's *two* list comprehensions. The >> interpreter computes the value if the inner list comprehension and >> then duplicates eight references to it (as you

Re: variable scope in list comprehensions

2008-04-04 Thread Piotr Sobolewski
Duncan Booth wrote: > For the OP, in some languages (e.g. C) 'for' loops typically calculate > the value of the loop control variable based on some expression > involving the previous value. Python isn't like that. In Python the data > used to compute the next value is stored internally: you canno

<    1   2   3   4   >