|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 - 3 | Next > |
|
|
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 |
|
|
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. <jbperez@...>: > 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 |
|
|
Re: substituting list comprehensions for map()On Mon, Nov 2, 2009 at 12:54 AM, Jon P. <jbperez@...> 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 |
|
|
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 -- http://mail.python.org/mailman/listinfo/python-list |
|
|
Re: substituting list comprehensions for map()"Jon P." <jbperez@...> 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 |
|
|
Re: substituting list comprehensions for map()"Jon P." <jbperez@...> 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 |
|
|
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... -- http://mail.python.org/mailman/listinfo/python-list |
|
|
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 -- http://mail.python.org/mailman/listinfo/python-list |
|
|
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 -- http://mail.python.org/mailman/listinfo/python-list |
|
|
Re: substituting list comprehensions for map()Bruno Desthuilliers <bruno.42.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. -- \ “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 |
|
|
Re: substituting list comprehensions for map()Ben Finney a écrit :
> Bruno Desthuilliers <bruno.42.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 :-/ -- http://mail.python.org/mailman/listinfo/python-list |
|
|
Re: substituting list comprehensions for map()Steven D'Aprano <steven@...> 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 |
|
|
Re: Pyfora, a place for pythonI just noticed the tag line "a place for Python". Looked it up online (http://pyfora.org/
) and it will be interesting to see if it can fill the void that I experience (no centralized place to post and view user submitted sample code) in the existing Python community. As for user community fragmentation, I would guess that someone would be less likely to create such a site if the user community needs were being met by the official sites. There is a place for the existing old school interaction forums (the IRC channel, the Usenet groups and mailing lists), but there is also a place for archived user submitted comments. My personal preference would be a link in each sub-paragraph in the official documentation to a wiki page devoted to that specific aspect of the Python language. A place were users could augment the documentation by providing sample code and by expanding out the documentation for those of us who don't live and breath Python in our sleep. Real Python coders would not click on the user wiki links and all of us newbies could communicate with each other. But until a place like that exists, perhaps Pyfora will get us part way there. Kee -- http://mail.python.org/mailman/listinfo/python-list |
|
|
|
|
|
Re: Pyfora, a place for python"Diez B. Roggisch" <deets@...> wrote:
> Kee Nethery wrote: >> My personal preference would be a link in each sub-paragraph in the >> official documentation to a wiki page devoted to that specific aspect >> of the Python language. A place were users could augment the >> documentation by providing sample code and by expanding out the >> documentation for those of us who don't live and breath Python in our >> sleep. Real Python coders would not click on the user wiki links and >> all of us newbies could communicate with each other. But until a >> place like that exists, perhaps Pyfora will get us part way there. > > This idea has been discussed before, and unfortunately not bore any > fruits so far - one of the few places PHP is actually better than > Python. So I'd love to see it happen. One option would be to use Google sidewiki. That way we need no changes to the existing site and people can add comments on pages or individual paragraphs, phrases or words. It's up and running today. However, so far as I know there isn't any easy way to find all sidewiki comments for a site: the APIs only allow you to retrieve comments for an individual page. (There's a sidewiki issue for this http://code.google.com/p/gdata-issues/issues/detail?id=1493 ) If they address this issue then the site could include a ticker of recent comments. Also of course some people may have objections to using sidewiki e.g. on privacy grounds. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list |
|
|
Re: substituting list comprehensions for map()J Kenneth King <james@...> writes:
> Steven D'Aprano <steven@...> 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 |
|
|
Re: substituting list comprehensions for map()On Tue, 03 Nov 2009 09:14:05 +1100, Ben Finney wrote:
> J Kenneth King <james@...> writes: > >> Steven D'Aprano <steven@...> 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 |
|
|
Re: substituting list comprehensions for map()On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote:
> "Jon P." <jbperez@...> 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 |
|
|
Re: substituting list comprehensions for map()> 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 |
|
|
Re: substituting list comprehensions for map()> 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 |
| < Prev | 1 - 2 - 3 | Next > |
| Free embeddable forum powered by Nabble | Forum Help |