[英]How do I add a string to another string and remove the overlapping part?
How do I add one string to another and remove a part in the middle if it's double? 如何将一个字符串添加到另一个字符串中,如果该字符串是双精度字符串,则如何删除中间的一部分?
I really don't know how to explain this but this is what I want to do: 我真的不知道该怎么解释,但这就是我想要做的:
Lets say 可以说
string1 = "abcde"
string2 = "cdefg"
How would I create a variable that's string1+string2 but with the "cde" part only once? 我如何创建一个字符串为string1 + string2但仅包含“ cde”部分的变量?
I'm looking for something like: 我正在寻找类似的东西:
string3 = merge(string1, string2)
That would make string3 "abcdefg" and not "abcdecdefg" 这将使string3为“ abcdefg”,而不是“ abcdecdefg”
I couldn't find it with google so that's why i'm asking here. 我在Google上找不到它,所以这就是我在这里问的原因。
Any help would be greatly appreciated :) 任何帮助将不胜感激 :)
You can check if there is an overlap first and then append only the non-overlapping parts: 您可以先检查是否存在重叠,然后仅附加不重叠的部分:
# Find overlap if there is any
for i in range(1, len(string2)):
if string1.endswith(string2[:i]):
k = i
# Concatenate strings:
string3 = string1 + (string2 if k is None else string2[k:])
Or even simpler, set k
to zero first: 甚至更简单,首先将
k
设置为零:
# Find overlap if there is any
k = 0
for i in range(1, len(string2)):
if string1.endswith(string2[:i]):
k = i
# Simply concatenate them
string3 = string1 + string2[k:]
We can look for each occurence of the first character of s2
in s1
, and test if the rest of s1
from this occurence is the start of s2
. 我们可以查找
s1
中s2
的第一个字符的每次出现,并测试此事件中其余的s1
是否是s2
的开始。 We need to do that from the start of s1
towards the end, in order to be sure to get the largest possible overlap. 我们需要从
s1
的开始到结束执行此操作,以确保获得最大的重叠。
import re
def fusion(s1, s2):
for m in re.finditer(s2[0], s1):
if s2.startswith(s1[m.start():]):
return s1[:m.start()] + s2
# no overlap found
return s1 + s2
string1 = "abede"
string2 = "edefg" # overlap: 'ede'
print(fusion(string1, string2))
# abedefg
This can be simply implemented with python set() as folows.. 这可以通过以下简单的python set()实现。
>>> string1 = "abcde"
>>> string2 = "cdefg"
>>> string3 = string1 + string2
>>> print (''.join(sorted(set(string3))))
abcdefg
Explanation:
set()
removes the duplicate elements,sorted()
sorts, and thejoin()
combines it all back.说明:
set()
删除重复的元素,对sorted()
排序,然后join()
将其重新组合在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.