Sort a sublist of elements in a list leaving the rest in place

前端 未结 15 2284
小蘑菇
小蘑菇 2021-02-13 14:28

Say I have a sorted list of strings as in:

[\'A\', \'B\' , \'B1\', \'B11\', \'B2\', \'B21\', \'B22\', \'C\', \'C1\', \'C11\', \'C2\']

Now I wan

15条回答
  •  鱼传尺愫
    2021-02-13 14:51

    def compound_sort(input_list, natural_sort_prefixes=()):
        padding = '{:0>%s}' % len(max(input_list, key=len))
        return sorted(
            input_list, 
            key = lambda li: \
                ''.join(
                    [li for c in '_' if not li.startswith(natural_sort_prefixes)] or 
                    [c for c in li if not c.isdigit()] + \
                    [c for c in padding.format(li) if c.isdigit()]
                )
            )
    

    This sort method receives:

    • input_list: The list to be sorted,
    • natural_sort_prefixes: A string or a tuple of strings.

    List items targeted by the natural_sort_prefixes will be sorted naturally. Items not matching those prefixes will be sorted lexicographically.

    This method assumes that the list items are structured as one or more non-numerical characters followed by one or more digits.

    It should be more performant than solutions using regex, and doesn't depend on external libraries.

    You can use it like:

    print compound_sort(['A', 'B' , 'B11', 'B1', 'B2', 'C11', 'C2'], natural_sort_prefixes=("A","B"))
    
    # ['A', 'B', 'B1', 'B2', 'B11', 'C11', 'C2']
    

提交回复
热议问题