substituting list comprehensions for map()
ben+python at benfinney.id.au
Tue Nov 3 05:55:20 CET 2009
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> On Mon, 02 Nov 2009 19:19:41 +1100, Ben Finney wrote:
> > "Jon P." <jbperez at gmail.com> 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 |
More information about the Python-list