What is the cleanest way to do a sort plus uniq on a Python list?

后端 未结 5 933
囚心锁ツ
囚心锁ツ 2020-12-23 15:55

Consider a Python list my_list containing [\'foo\', \'foo\', \'bar\'].

What is the most Pythonic way to uniquify and sort a list ?
(thi

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 16:50

    # Python ≥ 2.4
    # because of (generator expression) and itertools.groupby, sorted
    
    import itertools
    
    def sort_uniq(sequence):
        return (x[0] for x in itertools.groupby(sorted(sequence)))
    

    Faster:

    import itertools, operator
    import sys
    
    if sys.hexversion < 0x03000000:
        mapper= itertools.imap # 2.4 ≤ Python < 3
    else:
        mapper= map # Python ≥ 3
    
    def sort_uniq(sequence):
        return mapper(
            operator.itemgetter(0),
            itertools.groupby(sorted(sequence)))
    

    Both versions return an generator, so you might want to supply the result to the list type:

    sequence= list(sort_uniq(sequence))
    

    Note that this will work with non-hashable items too:

    >>> list(sort_uniq([[0],[1],[0]]))
    [[0], [1]]
    

提交回复
热议问题