Most Pythonic Way to Split an Array by Repeating Elements

前端 未结 11 1256
星月不相逢
星月不相逢 2021-02-13 09:51

I have a list of items that I want to split based on a delimiter. I want all delimiters to be removed and the list to be split when a delimiter occurs twice. F

11条回答
  •  渐次进展
    2021-02-13 10:04

    Here's a clean nice solution using zip and generators

    #1 define traditional sequence split function 
    #if you only want it for lists, you can use indexing to make it shorter
    def split(it, x):
        to_yield = []
        for y in it:
            if x == y:
                yield to_yield
                to_yield = []
            else:
                to_yield.append(y)
        if to_yield:
            yield to_yield
    
    #2 zip the sequence with its tail 
    #you could use itertools.chain to avoid creating unnecessary lists
    zipped = zip(l, l[1:] + [''])
    
    #3. remove ('X',not 'X')'s from the resulting sequence, and leave only the first position of each
    # you can use list comprehension instead of generator expression
    filtered = (x for x,y in zipped if not (x == 'X' and y != 'X'))
    
    #4. split the result using traditional split
    result = [x for x in split(filtered, 'X')]
    

    This way split() is more reusable.

    It's surprising python doesn't have one built in.

    edit:

    You can easily adjust it for longer split sequences, repeating steps 2-3 and zipping filtered with l[i:] for 0< i <= n.

提交回复
热议问题