如何加入字符串列表并删除重复的字母(将它们链接在一起) [英] How can I join a list of strings and remove duplicated letters (keep them chained)
问题描述
l = ["volcano", "noway", "lease", "sequence", "erupt"]
所需的输出:
'volcanowayleasequencerupt'
我尝试过:
使用itertools.groupby
,但当行中有2个重复的字母(即leasesequence
-> sese
停留)时,它似乎无法正常工作:
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'
如您所见,它仅在最后一个'e'
处被删除,因此这不是理想的,因为如果一个字母包含双字符,它们将缩小为1,即'suddenly'
变为'sudenly'
.
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'
.
我正在寻找最适合Python的方法.
I'm looking for the most Pythonic approach for this.
谢谢.
编辑
我的列表中没有重复的项目.
My list does not have any duplicated items in it.
推荐答案
我认为是更具可读性的版本:
A more readable version, in my opinion:
from functools import reduce
def max_overlap(s1, s2):
return next(
i
for i in reversed(range(len(s2) + 1))
if s1.endswith(s2[:i])
)
def overlap(strs):
return reduce(
lambda s1, s2:
s1 + s2[max_overlap(s1, s2):],
strs, '',
)
overlap(l)
#> 'volcanowayleasequencerupt'
但是,它还会考虑之前重叠的单词中的累积"字符:
However, it also considers "accumulated" characters from previous words that overlapped:
overlap(['split', 'it', 'lit'])
#> 'split'
这篇关于如何加入字符串列表并删除重复的字母(将它们链接在一起)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!