Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join a list of strings and remove duplicated letters (keep them chained)

My list:

l = ["volcano", "noway", "lease", "sequence", "erupt"]

Desired output:

'volcanowayleasequencerupt'

I have tried:

using itertools.groupby but it seems like it doesn't work well when there is 2 repeated letters in row (i.e. leasesequence -> sese stays):

>>> from itertools import groupby
>>> "".join([i[0] for i in groupby("".join(l))])
'volcanonowayleasesequencerupt'

As you can see it got rid only for the last 'e', and this is not ideal because if a letter has double characters they will be shrunk to 1. i.e 'suddenly' becomes 'sudenly'.

I'm looking for the most Pythonic approach for this.

Thank you in advance.

EDIT

My list does not have any duplicated items in it.

like image 575
hofD Avatar asked Mar 03 '23 21:03

hofD


1 Answers

Using a helper function that crops a word t by removing its longest prefix that's also a suffix of s:

def crop(s, t):
    for k in range(len(t), -1, -1):
        if s.endswith(t[:k]):
            return t[k:]

And then crop each word with its preceding word:

>>> l = ["volcano", "noway", "lease", "sequence", "erupt"]
>>> ''.join(crop(s, t) for s, t in zip([''] + l, l))
'volcanowayleasequencerupt'

>>> l = ['split', 'it', 'lit']
>>> ''.join(crop(s, t) for s, t in zip([''] + l, l))
'splitlit'
like image 165
Kelly Bundy Avatar answered Mar 06 '23 07:03

Kelly Bundy