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

Re: substituting list comprehensions for map()

2009-11-04 Thread Ben Finney
Steven D'Aprano ste...@remove.this.cybersource.com.au 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

Re: substituting list comprehensions for map()

2009-11-04 Thread J Kenneth King
Steven D'Aprano ste...@remove.this.cybersource.com.au writes: 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

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 ste...@remove.this.cybersource.com.au 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,

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,

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 with

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

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 construct at all

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. jbpe...@gmail.com: I'd like to do: resultlist = operandlist1 + operandlist2 where for example operandlist1=[1,2,3,4,5] operandlist2=[5,4,3,2,1] and

Re: substituting list comprehensions for map()

2009-11-02 Thread Chris Rebert
On Mon, Nov 2, 2009 at 12:54 AM, Jon P. jbpe...@gmail.com 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 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 Paul Rudin
Jon P. jbpe...@gmail.com 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)

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Jon P. jbpe...@gmail.com 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

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 Neil Crighton
Steven D'Aprano steven at 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, it

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 Ben Finney
Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid 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

Re: substituting list comprehensions for map()

2009-11-02 Thread Bruno Desthuilliers
Ben Finney a écrit : Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid 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

Re: substituting list comprehensions for map()

2009-11-02 Thread J Kenneth King
Steven D'Aprano ste...@remove.this.cybersource.com.au writes: 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].

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
J Kenneth King ja...@agentultra.com writes: Steven D'Aprano ste...@remove.this.cybersource.com.au writes: from operator import add map(add, operandlist1, operandlist2) This is the best solution so far. Strange to say it's a solution, when it doesn't solve the stated problem: to replace

Re: substituting list comprehensions for map()

2009-11-02 Thread Steven D'Aprano
On Tue, 03 Nov 2009 09:14:05 +1100, Ben Finney wrote: J Kenneth King ja...@agentultra.com writes: Steven D'Aprano ste...@remove.this.cybersource.com.au writes: from operator import add map(add, operandlist1, operandlist2) This is the best solution so far. Strange to say it's a

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. jbpe...@gmail.com 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

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 ifilter, imap

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

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Steven D'Aprano st...@remove-this-cybersource.com.au writes: On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote: Jon P. jbpe...@gmail.com writes: I'd like to do: resultlist = operandlist1 + operandlist2 That's an unfortunate way of expressing it; it's valid Python syntax

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
Anh Hai Trinh anh.hai.tr...@gmail.com 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

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

Re: substituting list comprehensions for map()

2009-11-02 Thread Sean DiZazzo
On Nov 2, 9:01 pm, Ben Finney ben+pyt...@benfinney.id.au wrote: Anh Hai Trinh anh.hai.tr...@gmail.com 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

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