sorting a tuple

Joshua Muskovitz josh at open.com
Tue Nov 14 21:51:36 EST 2000


> Can anyone tell me if there is any library function to srot a tuple ind
> ecending order? if not, can someone supply the code???

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> myTuple = ('45','234','345','23') # original is a tuple of strings>>>
myTuple
('45', '234', '345', '23')
>>> myList = list(myTuple) # convert it to a list so we can manipulate it
>>> myList
['45', '234', '345', '23']
>>> myList.sort() # sort it in place
>>> myList.reverse() # reverse it in place
>>> myList
['45', '345', '234', '23']
>>> # heh -- strings sort alphabetically, not numerically!
>>> import string
>>> # let's convert the tuple of string to a list of ints
>>> myOtherList = map(string.atoi, myTuple)
>>> myOtherList
[45, 234, 345, 23]
>>> myOtherList.sort() # sort it numerically this time
>>> myOtherList.reverse() # and reverse it
>>> myOtherList
[345, 234, 45, 23]
>>> # now they are sorted numerically!
>>> myFinalList = map(str, myOtherList) # let's convert them back to strings
>>> myFinalList
['345', '234', '45', '23']
>>> myFinalTuple = tuple(myFinalList)
>>> myFinalTuple
('345', '234', '45', '23')
>>>

Does that help?

-- josh






-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list