|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Sort using psql or python? Error!
I'm retrieving some fields using psql, sort it and then display it in python, I have some problem, and my query's something like this :
"select blah blah, round(sum(something)) from somewhere group by blah blah order by round(sum(something));" And when I display the result, something's not quite right, the results're not sorted in order, it showed something similar to this : result ------- 100.00 101.10 106.3 105.5 ... As you can see, some are in order, and some not, what seems to be the problem? I tried not to use order by and sort the results using list sorting in python, and the outcome is the same.
__________________
What can change the nature of a man? |
|
#2
|
||||
|
||||
|
They are being sorted as strings. If you convert the values to numeric and then sort, the order will be as you expect.
Note that the floating point numbers will not preserve well after the conversion - so you may need to do some formatting back to strings: Code:
>>> a = ['100.00','101.10','106.3','105.5'] >>> a ['100.00', '101.10', '106.3', '105.5'] >>> c = map(float,a) >>> c [100.0, 101.09999999999999, 106.3, 105.5] >>> c.sort() >>> c [100.0, 101.09999999999999, 105.5, 106.3] >>> Grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** Last edited by Grim Archon : March 31st, 2004 at 01:11 AM. |
|
#3
|
|||
|
|||
|
are you sorting them using your MySQL query or are you using Python to sort them? if you're using MySQL, check to make sure that it's grouping them correctly before Python prints them out.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Sort using psql or python? Error! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|