Sort strings by the first N characters

后端 未结 2 2084
野趣味
野趣味 2021-01-04 23:44

I have a text file with lines like this:

2010-02-18 11:46:46.1287 bla
2010-02-18 11:46:46.1333 foo
2010-02-18 11:46:46.1333 bar
2010-02-18 11:46:46.1467 bla
         


        
2条回答
  •  借酒劲吻你
    2021-01-05 00:16

    sorted(array, key=lambda x:x[:24])
    

    Example:

    >>> a = ["wxyz", "abce", "abcd", "bcde"]
    >>> sorted(a)
    ['abcd', 'abce', 'bcde', 'wxyz']
    >>> sorted(a, key=lambda x:x[:3])
    ['abce', 'abcd', 'bcde', 'wxyz']
    

提交回复
热议问题