Hi all,
I am a Python Newbie and started learning it with this book "Programming in Python 3", which has a lot of sample codes.
So I am using Python 3 and got stuck while trying this program.The program is to swap the elements in the Tuple and how to do it by using a function object.Sorted method is also used to sort the result.
def swap(t):
return t[1],t[0]
x=zip((1,3,1,3),("param","dorie","kayak","canoe"))
y=sorted(x,key=swap)
print('y=',y)
z=sorted(x)
print('z=',z)
l=sorted(x,key=swap)
print('l=',l)
While executing this program Python 3 gives an empty list for 'z' and 'l'.So i tried executing this program using Python 2.6 and got a different result which I assume is correct.These are the outputs
----------Python 3----------
C:\Python30\ExPython>c:\Python30\python.exe ExPFI.py
y= [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')]
z= []
l= []
----------Python2.6----------
C:\Python30\ExPython>c:\Python26\python.exe ExPFI.py
('y=', [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')])
('z=', [(1, 'kayak'), (1, 'param'), (3, 'canoe'), (3, 'dorie')])
('l=', [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')])
Can you please help me by explaining ,why 'z' and 'l' is getting the empty strings while running program with Python 3.
Thanks in Advance