By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,048 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,048 IT Pros & Developers. It's quick & easy.

Statement (un)equality

P: n/a
castle:/home/adam>python
Python 2.3 (#3, Aug 4 2003, 16:43:33)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[[y,x] for x,y in [1,2],[3,4]] [[2, 1], [4, 3]] map(lambda x,y: [y,x], [1,2],[3,4]) [[3, 1], [4, 2]]

Why there is a difference? How to make the same thing like in map statement?
Regards
Adam Przybyla
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Adam Przybyla wrote:
[[y,x] for x,y in [1,2],[3,4]] [[2, 1], [4, 3]] map(lambda x,y: [y,x], [1,2],[3,4]) [[3, 1], [4, 2]] Why there is a difference? How to make the same thing like in map
statement?


Because the same things aren't happening here, despite their outward
similarity. For the second case to be equivalent to the first, you
really meant:
map(lambda x: [x[1], x[0]], [[1, 2], [3, 4]]) [[2, 1], [4, 3]]

You can pass multiple sequences to map, and that interleaves the
results:
map(lambda x, y, z: (x, y, z), [1, 2], [3, 4], [5, 6])

[(1, 3, 5), (2, 4, 6)]

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Come not between the dragon and his wrath.
-- King Lear (Act I, Scene I)
Jul 18 '05 #2

P: n/a
> [Adam Przybyla]
[[y,x] for x,y in [1,2],[3,4]] [[2, 1], [4, 3]] map(lambda x,y: [y,x], [1,2],[3,4]) [[3, 1], [4, 2]]

def test(a,b): print "a:", a, "b:", b
return [b,a] map(test, [1,2], [3,4]) a: 1 b: 3
a: 2 b: 4
[[3, 1], [4, 2]] [test(a, b) for a, b in [1,2], [3,4]] a: 1 b: 2
a: 3 b: 4
[[2, 1], [4, 3]] [test(a, b) for a, b in zip([1,2], [3,4])]

a: 1 b: 3
a: 2 b: 4
[[3, 1], [4, 2]]

Jonas

Jul 18 '05 #3

P: n/a
On Friday 20 February 2004 10:31 am, Adam Przybyla wrote:
castle:/home/adam>python
Python 2.3 (#3, Aug 4 2003, 16:43:33)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[[y,x] for x,y in [1,2],[3,4]]
[[2, 1], [4, 3]]
map(lambda x,y: [y,x], [1,2],[3,4])
[[3, 1], [4, 2]]

Why there is a difference?
Hi,

[[y,x] for x,y in [1,2],[3,4]]

is just the same as:

[[y,x] for (x,y) in ([1,2],[3,4])]

The list comprehension regards "[1,2],[3,4]" as a single argument (a tuple)
and "x,y" is also a tuple. "x,y" is assigned first [1,2] and then [3,4].

In the map statement "[1,2],[3,4]" are two different arguments. map's
signature allows for any number of iterables to be passed after the first
argument.
How to make the same thing like in map
statement?
zip() gives you the sequence of pairs you want to pass: (zip() is very
similar to map with None as the first arguemnt. The difference is how they
handle sequences of unequal length.)
zip([1,2],[3,4])

[(1, 3), (2, 4)]

so:

[[y,x] for x,y in zip([1,2],[3,4])]

will match the behaviour of the map statement.
Regards
Adam Przybyla


James
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.