简体   繁体   中英

What is the most efficient way to add a special character after a sequence of repeated elements in a list?

I have a list:

lst= [1,1,2,2,3,3,4,4,5,5,5]

I need to prepare a list in this format

lst = [1,1,$,2,2,$,3,3,$,4,4,$,5,5,5]

This is the function I have used to form pairs of elements and add a special character when the pairs are not equal. This approach works well when the list size is small, but when bigger lists are considered, this is not the most efficient way.

附图

Also, is there a way where we can keep a track of the indices we are adding the special character at? Like maintaining another list just for the index values.

IIUC, you can use groupby and chain from itertools :

from itertools import groupby, chain
out = list(chain.from_iterable(list(g)+['$'] for _,g in groupby(lst)))[:-1]

output:

[1, 1, '$', 2, 2, '$', 3, 3, '$', 4, 4, '$', 5, 5, 5]

input:

lst = [1,1,2,2,3,3,4,4,5,5,5]

NB. do NOT use list as variable name, this shadows the list python builtin

alternative using a generator
def add_symbol(lst, symbol='$'):
    if lst:
        prev = lst[0]
    for item in lst:
        if item != prev:
            yield symbol
        prev = item
        yield item
            
out = list(add_symbol(lst))
getting the insertion indices as side effect
def add_symbol(lst, symbol='$', indices=None):
    if lst:
        prev = lst[0]
    insertions = 0
    for i, item in enumerate(lst):
        if item != prev:
            yield symbol
            if isinstance(indices, list):
                insertions += 1
                indices.append(i+insertions)
        prev = item
        yield item

idx = []
out = list(add_symbol(lst, indices=idx))

print(idx)
[3, 6, 9, 12]

print(out)
[1, 1, '$', 2, 2, '$', 3, 3, '$', 4, 4, '$', 5, 5, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM