Sort results non-lexicographically?

后端 未结 5 1732
轮回少年
轮回少年 2021-02-19 21:29

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.

5条回答
  •  广开言路
    2021-02-19 22:25

    1 - Install natsort module

    pip install natsort
    

    2 - Import natsorted

    >>> input = ['1', '10', '2', '0', '3', 'Hello', '100', 'Allowance']
    
    >>> from natsort import natsorted
    >>> natsorted(input)
    ['0', '1', '2', '3', '10', '100', 'Allowance', 'Hello']
    

    Source: https://pypi.python.org/pypi/natsort

提交回复
热议问题