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
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
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
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
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,
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
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
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
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
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
''.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
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
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:
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
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
[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
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
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
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:
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
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
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
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 = []
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:
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:
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
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):
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
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!)
--
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
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
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
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
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
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
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
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:
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
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:
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:
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:
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
42 matches
Mail list logo