Sorting Driving Crazy: URGENT: PLEASE HELP

TomH beans at bedford.net
Sat Mar 13 07:29:12 EST 2004


dont bother <dontbotherworld at yahoo.com> wrote in message news:<mailman.346.1079159911.19534.python-list at python.org>...
> This is really driving me crazy.
...
> 
> 
> When I print x and feature_vectors[x]  I get some of
> the entries which are not in the increasing order.
...
> 
> 22844 	4.61279461279
> SORTING
> 
> 22846 	93.1537598204
> SORTING
> 
> 2357 	0.00224466891134
> SORTING
> 
> 3105 	0.00224466891134
> SORTING
...
> 
> This should not happen but why is this happening and
> how to fix this.
> 
> My problem is given a dictionary, sort the keys and
> have a new dictionary with the key, values where the
> keys are in increasing order.
> 
> I would be really grateful if anyone can help me out
> 
> Thanks
> Dont
> 
...

Your keys are strings, so they're being sorted in alphabetical order
e.g. 'a', 'aa', 'b'. It doesn't matter if all of the characters in the
strings happen to be digits, e.g. '1', '11', '2', strings are strings.
Cast the keys to integers before the sort.

-Tom



More information about the Python-list mailing list