Home > Enterprise >  What is the most efficient way to add a special character after a sequence of repeated elements in a
What is the most efficient way to add a special character after a sequence of repeated elements in a

Time:05-05

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.

Attached image

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.

CodePudding user response:

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]
  • Related