以非字典顺序对结果进行排序? [英] Sort results non-lexicographically?
本文介绍了以非字典顺序对结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以一种易于理解的方式显示一些结果.出于这个问题的目的,其中一些是数字,一些是字母,有些是两者的组合.
I'm trying to display some results in a human-readable way. For the purposes of this question, some of them are numbers, some are letters, some are a combination of the two.
我试图弄清楚如何使它们像这样排序:
I'm trying to figure out how I could get them to sort like this:
input = ['1', '10', '2', '0', '3', 'Hello', '100', 'Allowance']
sorted_input = sorted(input)
print(sorted_input)
所需结果:
['0', '1', '2', '3', '10', '100', 'Allowance', 'Hello']
实际结果:
['0', '1', '10', '100', '2', '3', 'Allowance', 'Hello']
我在想办法时遇到了麻烦.
I'm having trouble coming up with how to do this.
推荐答案
1-安装natsort模块
pip install natsort
2-导入未排序的
>>> input = ['1', '10', '2', '0', '3', 'Hello', '100', 'Allowance']
>>> from natsort import natsorted
>>> natsorted(input)
['0', '1', '2', '3', '10', '100', 'Allowance', 'Hello']
来源: https://pypi.python.org/pypi/natsort
这篇关于以非字典顺序对结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文