这与删除重复字符或重复单词不同,如下所述。
Whitecaps'
’。
这里棘手的部分是它复制的地方的开始,而不是用一个空格分隔。所以我不能把它们分割成‘',删除重复的单词,然后加入。
我可以互相核对每封信。以<4个重复字母的阈值来限定为重复字母。
寻找一个更好的方法,但我错过了。
发布于 2020-06-16 20:10:03
像这样的怎么样?
def mangle(s):
for x in range(1, len(s)):
suffix = s[-x:]
if s.startswith(suffix):
return (s[:-x], suffix)
for case in [
"Blue Valley StarsBlue Valley",
"West Michigan WhitecapsWest Michigan",
"Oregon OraclesOregon",
"Hello World",
"123123",
]:
print(case, "->", mangle(case))输出:
Blue Valley StarsBlue Valley -> ('Blue Valley Stars', 'Blue Valley')
West Michigan WhitecapsWest Michigan -> ('West Michigan Whitecaps', 'West Michigan')
Oregon OraclesOregon -> ('Oregon Oracles', 'Oregon')
Hello World -> None
123123 -> ('123', '123')发布于 2020-06-16 20:09:33
考虑到您提供的信息,可以通过分割给定的大写字母而不是空格来实现这一点:
import re
list(set([x.strip() for x in re.findall('[A-Z][^A-Z]*', 'Blue Valley StarsBlue Valley')]))其中产出:
['Blue','Valley','Stars']如果希望使用单个字符串,请添加' '.join()
' '.join(list(set([x.strip() for x in re.findall('[A-Z][^A-Z]*', 'Blue Valley StarsBlue Valley')])))其中产出:
'Valley Stars Blue'发布于 2020-06-16 20:40:02
import re
def dedup (inText):
splitBySpace = inText.split()
final = []
for each in splitBySpace:
if each not in final:
checkFound = False
for eachSavedToken in final:
if eachSavedToken in each:
final.append(each.replace(eachSavedToken, ''))
checkFound = True
break
if not checkFound:
final.append(each)
return ' '.join(final)
for eachStr in ['Blue Valley StarsBlue Valley','West Michigan WhitecapsWest Michigan','Oregon OraclesOregon']:
print (dedup (eachStr))输出:
Blue Valley Stars
West Michigan Whitecaps
Oregon Oracles这假设顺序是重要的。它本质上是一种搜索。
https://stackoverflow.com/questions/62416702
复制相似问题