Sort a list of strings based on regular expression match

后端 未结 4 495
别跟我提以往
别跟我提以往 2021-01-14 02:33

I have a text file that looks a bit like:

random text random text, can be anything blabla %A blabla
random text random text, can be anything blabla %D blabla         


        
4条回答
  •  北海茫月
    2021-01-14 03:26

    Here is a quick-and-dirty approach. Without knowing more about the requirements of your sort, I can't know if this satisfies your need.

    Assume that your list is held in 'listoflines':

    listoflines.sort( key=lambda x: x[x.find('%'):] )
    

    Note that this will sort all lines without a '%' character by their final character.

提交回复
热议问题