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 because it refers to

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 resources, as a

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 of course to think

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 list, one

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 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

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 using a

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 this

Re: Misuse of list comprehensions?

2008-05-23 Thread duncan smith
Simon Forman wrote: On May 21, 4:36 am, Bruno Desthuilliers bruno. [EMAIL PROTECTED] 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

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 an

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

Re: Misuse of list comprehensions?

2008-05-21 Thread Simon Forman
On May 21, 4:36 am, Bruno Desthuilliers bruno. [EMAIL PROTECTED] 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

Misuse of list comprehensions?

2008-05-20 Thread John Salerno
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. Here's the two examples: Example 1: def compress(s): new = [] for c in s: if c not in new:

Re: Misuse of list comprehensions?

2008-05-20 Thread Diez B. Roggisch
John Salerno wrote: 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. Here's the two examples: Example 1: def compress(s): new = [] for c in s: if c not

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
[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 thus

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

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 in s if c not in

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. This line:

Re: Misuse of list comprehensions?

2008-05-20 Thread Thomas Bellman
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. So the append(c) is needed - and thus the

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. So the append(c) is

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 Paul McGuire
On May 20, 8:13 am, John Salerno [EMAIL PROTECTED] wrote: 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. Here's the two examples: Example 1: def compress(s):     new = []  

Re: Misuse of list comprehensions?

2008-05-20 Thread Arnaud Delobelle
Paul McGuire [EMAIL PROTECTED] writes: On May 20, 8:13 am, John Salerno [EMAIL PROTECTED] wrote: 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. Here's the two examples: Example 1:

Re: Misuse of list comprehensions?

2008-05-20 Thread s0suk3
On May 20, 9:58 am, Paul McGuire [EMAIL PROTECTED] wrote: On May 20, 8:13 am, John Salerno [EMAIL PROTECTED] wrote: 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. Here's the two examples:

Re: Misuse of list comprehensions?

2008-05-20 Thread Paul McGuire
On May 20, 10:17 am, Arnaud Delobelle [EMAIL PROTECTED] wrote: Paul McGuire [EMAIL PROTECTED] writes: On May 20, 8:13 am, John Salerno [EMAIL PROTECTED] wrote: 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

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 compress(s):  

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

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: [...] split hairs 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 closed the tag!) --

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 aware of

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 order

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 definitively an

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 referring to

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 square of the

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 to be some built-in

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 behavior. What's that

Re: Misuse of list comprehensions?

2008-05-20 Thread Paddy
On May 20, 3:58 pm, Paul McGuire [EMAIL PROTECTED] wrote: On May 20, 8:13 am, John Salerno [EMAIL PROTECTED] wrote: 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. Here's the two examples:

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 the

Re: Misuse of list comprehensions?

2008-05-20 Thread Colin J. Williams
Colin J. Williams wrote: John Salerno wrote: 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. Here's the two examples: Example 1: def compress(s): new = [] for c in s:

Re: Misuse of list comprehensions?

2008-05-20 Thread Larry Bates
John Salerno wrote: 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. Here's the two examples: Example 1: def compress(s): new = [] for c in s: if c not in new:

Re: Misuse of list comprehensions?

2008-05-20 Thread Colin J. Williams
John Salerno wrote: 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. Here's the two examples: Example 1: def compress(s): new = [] for c in s: if c not in new:

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 object, it