![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
It was al1986 who got me into this ...
The reversed() function returns an iterator object. Try invoking a next() method on this object. Alternatively, you can loop through the items by doing for i in mysortedlist: print i So I tried this ... x = [1,2,3,4] print "original list =", x rx = reversed(x) for i in rx: print i, print Then this ... x = [1,2,3,4] print "original list =", x rx = reversed(x) print "rx.next() =", rx.next() print "rx.next() =", rx.next() print "rx.next() =", rx.next() When I combine the two codes I get an error that does not like next() or rx ... x = [1,2,3,4] print "original list =", x rx = reversed(x) for i in rx: print i, print print "rx.next() =", rx.next() print "rx.next() =", rx.next() print "rx.next() =", rx.next()
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#2 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
x = iter(range(4))
for i in x:
print i
if i%2:
break
print "!!"
print x.next()
print x.next()
print x.next() # <--Boom--OH. |
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Thanks hydroxide!
When I did a few print rx to check, the iterator seemed to be unchanged.
__________________
I looked it up on the Intergnats! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|