Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - substituting list comprehensions for map()

 
Thread Tools Search this Thread
Old 11-02-2009, 07:54 AM   #1
Default 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 ?


Jon P.
  Reply With Quote
Old 11-02-2009, 07:58 AM   #2
Javier Collado
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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 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
  Reply With Quote
Old 11-02-2009, 07:59 AM   #3
Chris Rebert
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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, 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


Chris Rebert
  Reply With Quote
Old 11-02-2009, 08:13 AM   #4
Steven D'Aprano
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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


Steven D'Aprano
  Reply With Quote
Old 11-02-2009, 08:17 AM   #5
Paul Rudin
 
Posts: n/a
Default Re: substituting list comprehensions for map()
"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 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.)


Paul Rudin
  Reply With Quote
Old 11-02-2009, 08:49 AM   #6
Bruno Desthuilliers
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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...


Bruno Desthuilliers
  Reply With Quote
Old 11-02-2009, 09:24 AM   #7
Neil Crighton
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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




Neil Crighton
  Reply With Quote
Old 11-02-2009, 09:39 AM   #8
Diez B. Roggisch
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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


Diez B. Roggisch
  Reply With Quote
Old 11-02-2009, 11:29 AM   #9
Bruno Desthuilliers
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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 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 :-/


Bruno Desthuilliers
  Reply With Quote
Old 11-02-2009, 01:45 PM   #10
J Kenneth King
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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 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.


J Kenneth King
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Substituting RAMs damaged my own system Thaqalain Computer Support 3 01-12-2006 11:36 PM
Substituting AOL Wav files with MSN Wav files Thaqalain Computer Support 6 07-07-2005 09:17 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46