473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Statement (un)equality

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
3 1261
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
> [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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: shank | last post by:
I'm coming from the ASP worl and have no clue to javascript. In the following code, I'm trying to set the value for variable strPage, then use that in the redirect statement. The page does nothing....
2
by: Tristan | last post by:
Hi I'm trying to test if two Pen objects are equal as shown below Pen p1 = new Pen(Color.Black) Pen p2 = new Pen(Color.Black) if (p1.Equals(p2) Console.WriteLine("These pens equal")
6
by: Hardy | last post by:
One of my customers have a sql statement totaled more than 400 lines, about 40KB. when excuted, error arrised saying "SQL0101N The statement is too long or too complex". I tried one of his...
1
by: gi75research | last post by:
What should be a very simple function is going terribly wrong, and I don't know why. StartTime and EndTime are table values (formatted like "01:00A" or "02:00P"); DaypartStart and DaypartEnd are...
40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
6
by: benben | last post by:
I am a C++ guy recently migrated to C#. One of the thing I don't understand is assignment (=) and equality (==) operators. If I try to do the following: C a = new C; C b = a; Then I will get...
14
by: Robert Latest | last post by:
Hi guys, I'm sure this has been beaten to death on this newsgroup, but I can't find it in the CLC FAQ. Consider the following code: -------- double x = some_value_from_somewhere;
12
by: Philippe Martin | last post by:
Hi, This code works, but is it "appropriate" ? l_init = False if True == l_init and 1234 = l_value: print 'l_value is initialized' I know I can do this with a try but ...
0
by: Gary Herron | last post by:
Good Z wrote: Short answer: Use == for equality. Don't use "is". Ever! (Especially if you are a newbie.) Longer answer: In a dozen years of programming Python, the only time I use "is"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.