Python: split a list based on a condition?

前端 未结 30 2294
误落风尘
误落风尘 2020-11-22 06:56

What\'s the best way, both aesthetically and from a performance perspective, to split a list of items into multiple lists based on a conditional? The equivalent of:

30条回答
  •  孤城傲影
    2020-11-22 07:01

    Yet another solution to this problem. I needed a solution that is as fast as possible. That means only one iteration over the list and preferably O(1) for adding data to one of the resulting lists. This is very similar to the solution provided by sastanin, except much shorter:

    from collections import deque
    
    def split(iterable, function):
        dq_true = deque()
        dq_false = deque()
    
        # deque - the fastest way to consume an iterator and append items
        deque((
          (dq_true if function(item) else dq_false).append(item) for item in iterable
        ), maxlen=0)
    
        return dq_true, dq_false
    

    Then, you can use the function in the following way:

    lower, higher = split([0,1,2,3,4,5,6,7,8,9], lambda x: x < 5)
    
    selected, other = split([0,1,2,3,4,5,6,7,8,9], lambda x: x in {0,4,9})
    

    If you're not fine with the resulting deque object, you can easily convert it to list, set, whatever you like (for example list(lower)). The conversion is much faster, that construction of the lists directly.

    This methods keeps order of the items, as well as any duplicates.

提交回复
热议问题