Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

substituting list comprehensions for map()

 

 

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded


jbperez at gmail

Nov 1, 2009, 11:54 PM

Post #1 of 29 (772 views)
Permalink
substituting list comprehensions for map()

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 list comprehension ?
--
http://mail.python.org/mailman/listinfo/python-list


javier.collado at gmail

Nov 1, 2009, 11:58 PM

Post #2 of 29 (751 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Hello,

I'll do the following:
[op1+op2 for op1,op2 in zip(operandlist1, operandlist2)]

Best regards,
Javier

2009/11/2 Jon P. <jbperez [at] gmail>:
> 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 list comprehension ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 1, 2009, 11:59 PM

Post #3 of 29 (750 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

On Mon, Nov 2, 2009 at 12:54 AM, Jon P. <jbperez [at] gmail> 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, operandlist1, operandlist2)
>
> Is there any reasonable way to do this via a list comprehension ?

results = [x+y for x,y in zip(list1, list2)]

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 2, 2009, 12:13 AM

Post #4 of 29 (750 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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, operandlist1, operandlist2)


If the two lists are very large, it would be faster to use this:


from operator import add
map(add, operandlist1, operandlist2)


> 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 with itertools.izip.




--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


paul.nospam at rudin

Nov 2, 2009, 12:17 AM

Post #5 of 29 (745 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

"Jon P." <jbperez [at] gmail> 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 there any reasonable way to do this via a list comprehension ?

You can do it as a list comprehension e.g. like this:

[ x + y for x, y in zip(operandlist1, operandlist2)]

Note that there is some unnecessary list building going on here and it
may be better to use itertools.izip. (In python 3 zip returns an
iterator anyhow.)
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 12:19 AM

Post #6 of 29 (745 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

"Jon P." <jbperez [at] gmail> 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).

> map(lambda op1,op2: op1 + op2, operandlist1, operandlist2)
>
> Is there any reasonable way to do this via a list comprehension ?

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

For the above case, this is how it's done::

>>> operandlist1 = [1, 2, 3, 4, 5]
>>> operandlist2 = [5, 4, 3, 2, 1]
>>> resultlist = [(a + b) for (a, b) in zip(operandlist1, operandlist2)]
>>> resultlist
[6, 6, 6, 6, 6]

--
\ “Creativity can be a social contribution, but only in so far as |
`\ society is free to use the results.” —Richard Stallman |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


bruno.42.desthuilliers at websiteburo

Nov 2, 2009, 12:49 AM

Post #7 of 29 (744 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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". There are things I'd rather
not write as a list comprehension...
--
http://mail.python.org/mailman/listinfo/python-list


neilcrighton at gmail

Nov 2, 2009, 1:24 AM

Post #8 of 29 (743 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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 would be faster to use this:
>

If the two lists are *very* large and every element in each list has the same
type, you should use NumPy arrays (http://numpy.scipy.org/).

>>> import numpy
>>> operandlist1 = numpy.array([1, 2, 3, 4, 5])
>>> operandlist2 = numpy.array([5, 4, 3, 2, 1])
>>> resultlist = operandlist1 + operandlist2
>>> resultlist
array([6, 6, 6, 6, 6])


Neil


--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Nov 2, 2009, 1:39 AM

Post #9 of 29 (743 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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 + op2, operandlist1, operandlist2)
>
>
> If the two lists are very large, it would be faster to use this:
>
>
> from operator import add
> map(add, operandlist1, operandlist2)
>
>
>> 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 with itertools.izip.

And even more so if one needs the results one by one - then just use a
generator-expression.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 2:01 AM

Post #10 of 29 (743 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Bruno Desthuilliers <bruno.42.desthuilliers [at] websiteburo> 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...

That's why I qualified it as I did. I'd be interested to know a ‘map()’
usage where there isn't a correspondingly simple list comprehension;
that is, one that couldn't be done without being significantly more
complex than the corresponding ‘map()’ usage.

--
\ “If we have to give up either religion or education, we should |
`\ give up education.” —William Jennings Bryan, 1923-01 |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


bruno.42.desthuilliers at websiteburo

Nov 2, 2009, 3:29 AM

Post #11 of 29 (741 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Ben Finney a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers [at] websiteburo> 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...
>
> That's why I qualified it as I did. I'd be interested to know a ‘map()’
> usage where there isn't a correspondingly simple list comprehension;
> that is, one that couldn't be done without being significantly more
> complex than the corresponding ‘map()’ usage.

I know I've seen the case, and more than once, but I'm afraid I don't
have any example to publish here - I'd need to do quite a bit of
archeology :-/
--
http://mail.python.org/mailman/listinfo/python-list


james at agentultra

Nov 2, 2009, 5:45 AM

Post #12 of 29 (741 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Steven D'Aprano <steven [at] REMOVE> 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 do:
>>
>> map(lambda op1,op2: op1 + op2, operandlist1, operandlist2)
>
>
> If the two lists are very large, it would be faster to use this:
>
>
> from operator import add
> map(add, operandlist1, 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 with itertools.izip.

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.

List comprehensions are good for one-off transformations where it would
only create a one-time method for map to use.
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 2:14 PM

Post #13 of 29 (728 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

J Kenneth King <james [at] agentultra> writes:

> Steven D'Aprano <steven [at] REMOVE> 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 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 this case.

--
\ “He may look like an idiot and talk like an idiot but don't let |
`\ that fool you. He really is an idiot.” —Groucho Marx |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 2, 2009, 7:32 PM

Post #14 of 29 (718 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

On Tue, 03 Nov 2009 09:14:05 +1100, Ben Finney wrote:

> J Kenneth King <james [at] agentultra> writes:
>
>> Steven D'Aprano <steven [at] REMOVE> 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.

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, 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 this case.


You obviously don't do enough functional programming :)

Apart from an occasional brain-fart where I conflate map() with filter(),
I find them perfectly readable and sensible. The only advantages to list
comps are you can filter results with an if clause, and for simple
expressions you don't need to create a function. They're non-trivial
advantages, but for me readability isn't one of them.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 2, 2009, 8:04 PM

Post #15 of 29 (720 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote:

> "Jon P." <jbperez [at] gmail> 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).

True, but it is valid mathematical syntax if you interpret lists as
vectors. I'm sure there are languages where [1,2]+[3,4] will return
[4,6]. Possibly R or Mathematica?


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

Everyone forgets the multiple argument form of map.

map(func, s1, s2, s3, ...)

would need to be written as:

[func(t) for f in itertools.izip_longest(s1, s2, s3, ...)]

which I guess is relatively simple, but only because izip_longest() does
the hard work.


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.

And I don't think you could write this as a single map:

[func(a, b) for a in seq1 for b in seq2]


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


anh.hai.trinh at gmail

Nov 2, 2009, 8:06 PM

Post #16 of 29 (708 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

> 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(*args))

vectorsum([1,2], [3,4], [5,6])
->[9, 12]
vectorsum([1,2], [3,4], [5,6], [7,8])
->[16, 20]

Peace,

----aht
--
http://mail.python.org/mailman/listinfo/python-list


anh.hai.trinh at gmail

Nov 2, 2009, 8:24 PM

Post #17 of 29 (716 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

> 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 there to reduce memory.


> And I don't think you could write this as a single map:
>
> [func(a, b) for a in seq1 for b in seq2]

Oh you surely could:

seq1, seq2 = [1,2,3], [4,5,6]

[a+b for a in seq1 for b in seq2]
->[5, 6, 7, 6, 7, 8, 7, 8, 9]

from itertools import product
map(sum, product(seq1, seq2))
->[5, 6, 7, 6, 7, 8, 7, 8, 9]


Peace,

----aht
--
http://mail.python.org/mailman/listinfo/python-list


anh.hai.trinh at gmail

Nov 2, 2009, 8:51 PM

Post #18 of 29 (717 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

> 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://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 8:55 PM

Post #19 of 29 (718 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Steven D'Aprano <steve [at] REMOVE-THIS-cybersource> writes:

> On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote:
>
> > "Jon P." <jbperez [at] gmail> 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).
>
> True, but it is valid mathematical syntax if you interpret lists as
> vectors. I'm sure there are languages where [1,2]+[3,4] will return
> [4,6]. Possibly R or Mathematica?

Python isn't one of them, which is why I cautioned strongly against
presenting it that way in this forum.

> Everyone forgets the multiple argument form of map.
>
> map(func, s1, s2, s3, ...)
>
> would need to be written as:
>
> [func(t) for f in itertools.izip_longest(s1, s2, s3, ...)]
>
> which I guess is relatively simple, but only because izip_longest() does
> the hard work.

Yes, I would call that equivalently simple. (I also find it more
explicit and hence readable.)

--
\ “Laurie got offended that I used the word ‘puke’. But to me, |
`\ that's what her dinner tasted like.” —Jack Handey |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 9:01 PM

Post #20 of 29 (717 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Anh Hai Trinh <anh.hai.trinh [at] gmail> 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:
>
> vectorsum = lambda *args: map(sum, zip(*args))

By “this” I take you to mean “the usage of ‘map’ in this code”, since
that's the limit of my question.

>>> vectorsum = lambda *args: [sum(items) for items in zip(*args)]
>>> vectorsum([1,2], [3,4], [5,6])
[9, 12]
>>> vectorsum([1,2], [3,4], [5,6], [7,8])
[16, 20]

--
\ “The apparent lesson of the Inquisition is that insistence on |
`\ uniformity of belief is fatal to intellectual, moral, and |
_o__) spiritual health.” —_The Uses Of The Past_, Herbert J. Muller |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 2, 2009, 9:31 PM

Post #21 of 29 (716 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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

[sum(t) for t in zip(*args)] will do it.

>>> args = [1,2], [3,4], [5,6]
>>> [sum(t) for t in zip(*args)]
[9, 12]
>>> args = [1,2], [3,4], [5,6], [7,8]
>>> [sum(t) for t in zip(*args)]
[16, 20]



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


half.italian at gmail

Nov 2, 2009, 10:54 PM

Post #22 of 29 (709 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

On Nov 2, 9:01pm, 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 simple list
> > > comprehension?)
>
> > Try turning this into a list comprehension:
>
> > vectorsum = lambda *args: map(sum, zip(*args))
>
> By this I take you to mean the usage of map in this code, since
> that's the limit of my question.
>
> >>> vectorsum = lambda *args: [sum(items) for items in zip(*args)]
> >>> vectorsum([1,2], [3,4], [5,6])
> [9, 12]
> >>> vectorsum([1,2], [3,4], [5,6], [7,8])
> [16, 20]
>
> --
> \ The apparent lesson of the Inquisition is that insistence on |
> `\ uniformity of belief is fatal to intellectual, moral, and |
> _o__) spiritual health. _The Uses Of The Past_, Herbert J. Muller |
> Ben Finney

prickly
--
http://mail.python.org/mailman/listinfo/python-list


james at agentultra

Nov 3, 2009, 7:22 AM

Post #23 of 29 (674 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

Ben Finney <ben+python [at] benfinney> writes:

> J Kenneth King <james [at] agentultra> writes:
>
>> Steven D'Aprano <steven [at] REMOVE> 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.

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 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 this case.

The use of map expresses a value and implies the procedure by which it
is obtained.

The list comprehension expresses the procedure by which the value is
obtained.

Both have uses and in some cases a list comprehension is definitely
preferrable to a map operation.

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 extra name bindings and iteration keywords. They
won't make our idea any more clear.

dot_product = map(mul, vec1, vec2)

vs

dot_product = [a * b for a, b in zip(vec1, vec2)]

It's very clear, at least to me, what a dot-product is in this case.
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 each element in a vector sequence. It's more succinct
to simply think of the value rather then expressing the procedure to
construct that value.

This isn't a hammer issue. Not every problem is a nail.
--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 3, 2009, 5:53 PM

Post #24 of 29 (658 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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 extra name bindings and iteration keywords. They
> won't make our idea any more clear.
>
> dot_product = map(mul, vec1, vec2)
>
> vs
>
> dot_product = [a * b for a, b in zip(vec1, vec2)]
>
> It's very clear, at least to me, what a dot-product is in this case.

Except it's not.

The dot product of two vectors returns a scalar, not another vector:
http://en.wikipedia.org/wiki/Dot_product

So what you want is:

dot_product = sum(map(mul, vec1, vec2))



> 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 each element in a vector sequence.

What you need is to define a function dot-product, and not hijack the
name for a local value. Then the function's implementation is irrelevant
to you: it could use a list comp, or could use map, it could use a for-
loop, a while loop, recursion, or black magic:

scalar = dot_product(vec1, vec2)


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


robert.kern at gmail

Nov 3, 2009, 8:43 PM

Post #25 of 29 (664 views)
Permalink
Re: substituting list comprehensions for map() [In reply to]

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 each element in a vector sequence.
>
> What you need is to define a function dot-product, and not hijack the
> name for a local value. Then the function's implementation is irrelevant
> to you: it could use a list comp, or could use map, it could use a for-
> loop, a while loop, recursion, or black magic:
>
> scalar = dot_product(vec1, vec2)

Or use the appropriate libraries:

from numpy import dot

scalar = dot(vec1, vec2)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.