Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 30th, 2007, 2:57 PM   #1
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
Sorting integer lists

Im studying python and I am unsure of how to solve a problem.

Im taking user input of two strings consisting of integers. I split these on whitespace into two lists, merge the lists, and sort it. My problem is that these lists are being sorted by character and not integers (i.e. 22 > 3).

Ive searched for a solution to this but have yet to find one. Does Python know these are characters? Can I convert each value, element-by-element into integers and expect it to sort them numerically?

Thanks.
sixstringartist is offline   Reply With Quote
Old Sep 30th, 2007, 3:50 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
>>> int("23")
23
>>> "23" * 2
'2323'
>>> int("23") * 2
46
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 1st, 2007, 12:26 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Some additional useful code:

python Syntax (Toggle Plain Text)
  1. >>> xs = ["2", "13", "3"]
  2. >>> xs.sort()
  3. >>> xs
  4. ['13', '2', '3']
  5.  
  6. >>> ys = map(int, xs)
  7. >>> ys
  8. [13, 2, 3]
  9. >>> ys.sort()
  10. >>> ys
  11. [2, 3, 13]
  12.  
  13. >>> zs = [int(i) for i in x]
  14. >>> zs
  15. [13, 2, 3]
  16. >>> zs.sort()
  17. [2, 3, 13]
Arevos is offline   Reply With Quote
Old Oct 5th, 2007, 4:08 PM   #4
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
Thanks DaWei but I was already aware that the issue was with they type of data in the list. What I was looking for was an approach like Arevos posted. I resorted to converting each value one by one to an int, but I was looking for a built-in way. It seems "map()" is what I was looking for. I'll read more about it.
sixstringartist is offline   Reply With Quote
Old Dec 4th, 2007, 2:27 PM   #5
somehollis
Programmer
 
somehollis's Avatar
 
Join Date: May 2006
Location: Memphis, TN
Posts: 31
Rep Power: 0 somehollis is on a distinguished road
Send a message via AIM to somehollis
Re: Sorting integer lists

I realize this thread is rather old, but for reference, you can also use your list's sort() method if you add a parameter to it.
numList = ["12","4","66","55"]
numList.sort(cmp = lambda x,y: int(x) - int(y))
# returns ['4','12','55',66']
somehollis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Update IdTCPSDerver in a DLL sggaunt Delphi 0 Mar 13th, 2007 11:56 AM
VB Phone Number to Name... NDawg28 Visual Basic 9 Mar 10th, 2007 9:40 PM
Sorting lists Sane Python 21 May 5th, 2005 6:39 PM
Linking dll failure (file not found) amrfathy_egy Visual Basic 8 Feb 3rd, 2005 3:13 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC