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
Steven D'Aprano 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 express it accordingly. There's

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
Ben Finney writes: > J Kenneth King writes: > >> Steven D'Aprano 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 use of ‘map()’ w

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
On Tue, 03 Nov 2009 09:14:05 +1100, Ben Finney wrote: > J Kenneth King writes: > >> Steven D'Aprano 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 >

Re: substituting list comprehensions for map()

2009-11-02 Thread Ben Finney
J Kenneth King writes: > Steven D'Aprano 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 use of ‘map()’ with a list comprehension. > I unde

Re: substituting list comprehensions for map()

2009-11-02 Thread J Kenneth King
Steven D'Aprano 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]. Using map(), I can

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