So, is there any explanation for this?Why can't python do a numerical sort?

Click For Summary

Discussion Overview

The discussion revolves around the ability of Python to perform numerical sorting on arrays, particularly in comparison to Perl. Participants explore methods for sorting lists that contain numerical strings and express frustrations regarding the perceived limitations of Python's sorting capabilities.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses surprise that Python cannot perform a numerical sort as easily as Perl, seeking a simpler solution than using external libraries like NumPy.
  • Another participant suggests using the sort method with a custom comparison function to sort the list numerically.
  • A participant clarifies that if the list contains strings formatted as "number text", the numerical part can be extracted using string manipulation before sorting.
  • Some participants argue that Python does sort numerically when the list contains actual integers, and provide examples to demonstrate this functionality.
  • There is a suggestion that Python's sorting capabilities are not as intuitive as those in Perl, leading to calls for a built-in numerical sort function.
  • One participant challenges the initial claim of Python's limitations, asserting that the sorting works correctly on various platforms when integers are used directly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether Python's sorting capabilities are adequate. Some believe that Python's sorting is sufficient when used correctly, while others maintain that it is less straightforward compared to Perl.

Contextual Notes

Participants express varying levels of familiarity with Python's documentation, which may influence their understanding of sorting functionality. There are also mentions of specific code examples that work under certain conditions, but these examples do not resolve the broader debate about ease of use.

Who May Find This Useful

Individuals interested in programming with Python, particularly those transitioning from Perl or seeking to understand sorting mechanisms in Python.

nameta9
Messages
185
Reaction score
0
No numerical sort in python ?

I was really surprised to see that python can't do such an elementary operation!

I am trying to do a numerical sort in python on an array. I am using python for windows (DOS). In perl you can simply write sort{$a<=>$b}@a and this will sort it according to the value of the numerical string, (not the alphabetical value). I am looking for something similar in python but could only find a reference to sourceforge numpy where I have to download a large piece of source code in C and recompile it locally. Is there a simpler solution ? thanks...
 
Last edited:
Technology news on Phys.org
x.sort(lambda a,b: cmp(int(a), int(b)))

where x is the list you want to sort. Of course, you could just say x.sort() if you just stored your list of numbers as an actual list of numbers and not a list of strings.
 
Thanks master. Getting close but it doesn't seem to work. My list is of the format x[1]="1123 wwww", x[2]="345 asdd", etc. What should I put in int(a) and int(b) ? when this is sorted "1123 wwwww" is before "345 asdd" because it is doing an alphabetical sort and not a numerical sort. Thanks for any suggestions.
 
Try int(a.split()[0]) and int(b.split()[0]).
 
It doesn't even do this! I also tried with a.append(22) etc. I don't think python sorts numerically in any case! Maybe it is because python on windows is too immature ?

a=[]
a.append(int('22'))
a.append(int('111'))
a.append(int('9'))
a.append(int('12'))

a.sort()

for mat in a:print mat


Second EDIT::::

OK it has to be done like this:

def descendingCmp(a,b):
return cmp(a,b)

a=[]
a.append(22)
a.append(111)
a.append(9)
a.append(12)

a.sort(descendingCmp)

for mat in a:print mat


It is simpler in perl, maybe they should simply create a built in function for sort numeric...
 
Last edited:
So, you are decrying python because you did not take the time to even quickly read through the python docs? You don't need to declare variable types in python. 4 is a number and '4' is a string.

Here, try this:
Code:
a=[123,234,542,821,111,54,767,23453,90,9,0,1,321]
a
a.sort()
a
it will work on windows, mac, linux, unix...

If you really want to get crazy do this:
Code:
a=[(123,'jack'),(1,'work'),(234,'a'),(542,'boy'),(111,'makes'),(90,'play'),(9,'and'),(0,'all'),(321,'dull'),(54,'no')]
a
a.sort()
a
it's easy.

Here is a good source for python knowledge:
http://www.python.org/doc/2.4.1/tut/tut.html

as is this:
http://diveintopython.org/

You should thumb through the docs and brush up a little. Python is day and night easier than perl.
 
Last edited by a moderator:
nameta9 said:
It doesn't even do this! I also tried with a.append(22) etc. I don't think python sorts numerically in any case! Maybe it is because python on windows is too immature ?

a=[]
a.append(int('22'))
a.append(int('111'))
a.append(int('9'))
a.append(int('12'))

a.sort()

for mat in a:print mat

This code works fine for me. When I run it I get:

9
12
22
111
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
7
Views
4K
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K