【问题标题】:How to remove/delete characters from end of string that match another end of string如何从与字符串的另一端匹配的字符串末尾删除/删除字符
【发布时间】:2019-04-16 10:22:21
【问题描述】:

我有数千个采用这种格式的字符串(不是英文):

['MyWordMyWordSuffix', 'SameVocabularyItemMyWordSuffix']

我想返回以下内容:

['MyWordMyWordSuffix', 'SameVocabularyItem']

因为字符串是不可变的,我想从最后开始匹配,所以我一直对如何处理它感到困惑。

我最好的猜测是某种从字符串末尾开始并不断检查匹配项的循环。

但是,由于我有这么多要处理的内容,似乎应该有一种内置方式比循环遍历所有字符更快,但由于我仍在学习 Python,所以我不知道一个(还) .

我可以在 SO 上找到的最接近的示例可以找到 here,但这并不是我真正想要的。

谢谢你帮助我!

【问题讨论】:

  • 如何准确定义“字符串结尾”?
  • 再提供2-3个例子来说明一下

标签: python-3.x string replace nltk


【解决方案1】:

您可以使用commonprefix from os.path 来查找它们之间的共同后缀:

from os.path import commonprefix

def getCommonSuffix(words):
    # get common suffix by reversing both words and finding the common prefix
    prefix = commonprefix([word[::-1] for word in words])
    return prefix[::-1]

然后您可以使用它从列表的第二个字符串中切出后缀:

word_list = ['MyWordMyWordSuffix', 'SameVocabularyItemMyWordSuffix']

suffix = getCommonSuffix(word_list)
if suffix:
    print("Found common suffix:", suffix)

    # filter out suffix from second word in the list
    word_list[1] = word_list[1][0:-len(suffix)]
    print("Filtered word list:", word_list)
else:
    print("No common suffix found")

输出:

Found common suffix: MyWordSuffix
Filtered word list: ['MyWordMyWordSuffix', 'SameVocabularyItem']

演示:https://repl.it/@glhr/55705902-common-suffix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2023-03-29
    相关资源
    最近更新 更多