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, 03:24 PM   #11
Default Re: Pyfora, a place for python


I 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




Kee Nethery
  Reply With Quote
Old 11-02-2009, 03:38 PM   #12
Diez B. Roggisch
 
Posts: n/a
Default Re: Pyfora, a place for python
Kee Nethery wrote:

> I 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.


ASPN cookbook?

And I don't think that a phpBB (or commercial rip-off) forum can be good at
that - the search-function of these things sucks big time, and
classification through tagging or hierarchical organization is also not
possible.

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

However I totally fail to see how the pyfora are any step into that
direction.

Diez


Diez B. Roggisch
  Reply With Quote
Old 11-02-2009, 05:39 PM   #13
Paul Rubin
 
Posts: n/a
Default Re: Pyfora, a place for python
Kee Nethery <> writes:
> I 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.


Something wrong with pastebin.com or any of its lookalikes?


Paul Rubin
  Reply With Quote
Old 11-03-2009, 03:32 AM   #14
Steven D'Aprano
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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
> 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


Steven D'Aprano
  Reply With Quote
Old 11-03-2009, 04:04 AM   #15
Steven D'Aprano
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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
> ‘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


Steven D'Aprano
  Reply With Quote
Old 11-03-2009, 04:06 AM   #16
Anh Hai Trinh
 
Posts: n/a
Default 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


Anh Hai Trinh
  Reply With Quote
Old 11-03-2009, 04:24 AM   #17
Anh Hai Trinh
 
Posts: n/a
Default 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


Anh Hai Trinh
  Reply With Quote
Old 11-03-2009, 04:51 AM   #18
Anh Hai Trinh
 
Posts: n/a
Default Re: substituting list comprehensions for map()
> 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)]


Anh Hai Trinh
  Reply With Quote
Old 11-03-2009, 05:31 AM   #19
Steven D'Aprano
 
Posts: n/a
Default Re: substituting list comprehensions for map()
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


Steven D'Aprano
  Reply With Quote
Old 11-03-2009, 06:54 AM   #20
Sean DiZazzo
 
Posts: n/a
Default Re: substituting list comprehensions for map()
On Nov 2, 9:01*pm, 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


Sean DiZazzo
  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