按升序对数字字符串列表进行排序

[英]Sort a list of numerical strings in ascending order


I have created a sqlite database which has a table which stores temperature values. The temperature values are written to the database in ascending order for the first time. Then I read the temperature values from the database into a list and then add that list to a combo box to select temperatures - works fine.

我创建了一个sqlite数据库,它有一个存储温度值的表。温度值首次以升序写入数据库。然后我将数据库中的温度值读入列表,然后将该列表添加到组合框中以选择温度 - 工作正常。

The resulting list is, say:

结果列表是:

templist = ['25', '50', '100', '150', '200', '250', '300'].

Then I add a new temperature value, say, '33' to the database.

然后我向数据库添加一个新的温度值,比如'33'。

It gets appended to the end of the table. If I read the temperatures now, the list will become:

它被附加到表的末尾。如果我现在读取温度,列表将变为:

['25', '50', '100', '150', '200', '250', '300', '33']. 

If I do templist.sort() or sorted(templist), the end result is

如果我执行templist.sort()或sorted(templist),最终结果是

['150', '200', '25', '250', '300', '33', '50']

Is there any simple way to sort the list in ascending order so that I get:

是否有任何简单的方法按升序对列表进行排序,以便我得到:

['25', '33', '50', '100', '150', '200', '250', '300']

2 个解决方案

#1


66  

The recommended approach in this case is to sort the data in the database, adding an ORDER BY at the end of the query that fetches the results, something like this:

在这种情况下,推荐的方法是对数据库中的数据进行排序,在查询结束时添加ORDER BY以获取结果,如下所示:

SELECT temperature FROM temperatures ORDER BY temperature ASC;  -- ascending order
SELECT temperature FROM temperatures ORDER BY temperature DESC; -- descending order

If for some reason that is not an option, you can change the sorting order like this in Python:

如果由于某种原因不能选择,您可以在Python中更改这样的排序顺序:

templist = [25, 50, 100, 150, 200, 250, 300, 33]
sorted(templist, key=int)               # ascending order
> [25, 33, 50, 100, 150, 200, 250, 300]
sorted(templist, key=int, reverse=True) # descending order
> [300, 250, 200, 150, 100, 50, 33, 25]

As has been pointed in the comments, the int key (or float if values with decimals are being stored) is required for correctly sorting the data if the data received is of type string, but it'd be very strange to store temperature values as strings, if that is the case, go back and fix the problem at the root, and make sure that the temperatures being stored are numbers.

正如在注释中所指出的那样,如果收到的数据是字符串类型,则正确排序数据时需要int键(如果存储了带小数的值,则为float),但将温度值存储为非常奇怪字符串,如果是这种情况,请返回并修复根目录中的问题,并确保存储的温度是数字。

#2


25  

in python sorted works like you want with integers:

在python中排序的工作就像你想要的整数:

>>> sorted([10,3,2])
[2, 3, 10]

it looks like you have a problem because you are using strings:

看起来你有问题,因为你正在使用字符串:

>>> sorted(['10','3','2'])
['10', '2', '3']

(because string ordering starts with the first character, and "1" comes before "2", no matter what characters follow) which can be fixed with key=int

(因为字符串排序从第一个字符开始,“1”在“2”之前,无论后面是什么字符)可以用key = int修复

>>> sorted(['10','3','2'], key=int)
['2', '3', '10']

which converts the values to integers during the sort (it is called as a function - int('10') returns the integer 10)

在排序期间将值转换为整数(它被称为函数 - int('10')返回整数10)

and as suggested in the comments, you can also sort the list itself, rather than generating a new one:

并且根据评论中的建议,您还可以对列表本身进行排序,而不是生成新列表:

>>> l = ['10','3','2']
>>> l.sort(key=int)
>>> l
['2', '3', '10']

but i would look into why you have strings at all. you should be able to save and retrieve integers. it looks like you are saving a string when you should be saving an int? (sqlite is unusual amongst databases, in that it kind-of stores data in the same type as it is given, even if the table column type is different).

但我会研究为什么你有字符串。你应该能够保存和检索整数。你应该保存一个int,看起来你正在保存一个字符串? (sqlite在数据库中是不常见的,因为它存储的数据与给定的类型相同,即使表列类型不同)。

and once you start saving integers, you can also get the list back sorted from sqlite by adding order by ... to the sql command:

一旦你开始保存整数,你也可以通过向sql命令添加order ...来从sqlite中获取列表。

select temperature from temperatures order by temperature;

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/03/18/d723c89266267519af376a6bb05b3423.html



 
粤ICP备14056181号  © 2014-2019 ITdaan.com

赞助商广告