954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Python 3

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

pvi101
Newbie Poster
4 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

In Python 3.0 the built-in method zip() contains an iterable instead of a list (much like the new range() function)... meaning that after the first sorted() call, the internal pointer of the zip object is pointed at the END position of the object.

I don't think it's possible to "reset" the iterator without simply re-zipping the two lists....
HTH

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

Or, convert the zip to a list.

def swap(t):
    return t[1],t[0]

x=zip((1,3,1,3),("param","dorie","kayak","canoe"))

w=list(x)
print("w=", w)

y=sorted(w,key=swap)
print('y=',y)

z=sorted(w)
print('z=',z)

l=sorted(w,key=swap)
print('l=',l)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Does your book "Programming in Python 3" refer to Python version 3.0 or is it simply the third edition of the book? If it refers to version 3.0+ then the sample code is buggy!

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Looks like sample code is buggy:
Product Description

Python 3 is the best version of the language yet: It is more powerful, convenient, consistent, and expressive than ever before. Now, leading Python programmer Mark Summerfield demonstrates how to write code that takes full advantage of Python 3’s features and idioms. The first book written from a completely “Python 3” viewpoint, Programming in Python 3 brings together all the knowledge you need to write any program, use any standard or third-party Python 3 library, and create new library modules of your own. Source

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

Jim ,woooee,sneekula

Thanks a Mil for your help..

sneekula
Programmming in Python3 is the first book on Python3000 in the market.It's a great book.
The program which I posted is not a sample code from the text.All the sample codes in the book which I tried until now is working properly.

I got this bug when i modified a one of the examples in the book ,to try different things.

regards

pvi101
Newbie Poster
4 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

pvi101, welcome to the forum.
Looks like this might be a good book after all!
Hope to see you back soon asking the kind of questions like this one. We will all try to help and learn in the process with you.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 
pvi101, welcome to the forum. Looks like this might be a good book after all! Hope to see you back soon asking the kind of questions like this one. We will all try to help and learn in the process with you.

Thank You and I will,
Thinking about this error again for sometime.I am wondering whether ,the behaviour of zip object in both Python3 and Python2.6 should be the same?

if the internal pointer is not moving after the first sorted call to zip object ,that is kind of a bug in the python3 itself?Am i right on this.

Regards

pvi101
Newbie Poster
4 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This was a change from python 2.X to 3.0 to provide better performance.

In Python 2.X versions, zip() returned a list:

>>> zip(['Blue', 'Bunny', 'Red'],['1','2','3'])
[('Blue', '1'), ('Bunny', '2'), ('Red', '3')]
>>>


However for the sake of saving time, in 3.0 they decided to change the behavior so that zip() returned an iterator instead. The same thing happened with range()... if you type range(5) into 3.0 you'll get an iterator; yet 2.X gives this:

>>> range(5)
[0, 1, 2, 3, 4]
>>>

Try it!
**Note: range() in Python 3.0 is actually xrange() from python 2.X**

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

This was a change from python 2.X to 3.0 to provide better performance.

In Python 2.X versions, zip() returned a list:

>>> zip(['Blue', 'Bunny', 'Red'],['1','2','3'])
[('Blue', '1'), ('Bunny', '2'), ('Red', '3')]
>>>

However for the sake of saving time, in 3.0 they decided to change the behavior so that zip() returned an iterator instead. The same thing happened with range()... if you type range(5) into 3.0 you'll get an iterator; yet 2.X gives this:

>>> range(5)
[0, 1, 2, 3, 4]
>>>

Try it! **Note: range() in Python 3.0 is actually xrange() from python 2.X**

Many Thanks jlm699

pvi101
Newbie Poster
4 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
supernovicevn
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You