[英]Python does not sort sql query result
results = conn.execute(SEARCH_SQL, dict(fingerprint="{"+fp_str+"}")).fetchall() print sorted(results)
I retrieve some datas from database by using sql alchemy. 我使用sql alchemy从数据库中检索了一些数据。 results
is like that: results
是这样的:
[(0.515625, u'str1'), (0.625, u'str2'), (0.901042, u'str3')]
However sort function does not work here, that is it does not do any operation on the list returned from sql query? 但是sort函数在这里不起作用,就是说它对sql查询返回的列表不做任何操作? How can I sort result list? 如何排序结果列表?
You have a list of tuples. 您有一个元组列表。 How would you like to sort them? 您想如何对它们进行排序?
For example, if you want to sort them according to the first key: 例如,如果要根据第一个键对它们进行排序:
sorted(results, key=lambda t:t[0])
or in reverse order: 或以相反的顺序:
sorted(results, key=lambda t:t[0], reverse=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.