![]() |
|
|
|||||||
![]() |
Python - substituting list comprehensions for map() |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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. |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
"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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |