繁体   English   中英

如果子字符串重复,如何获取字符串中子字符串的周围单词?

[英]How to get surrounding words of substring in string, if the substring repeats itself?

我有一个任务,我需要在字符串中的每个子字符串(可能是多个单词)之前和之后获取 N 个单词。 我最初考虑使用 str.split(" ") 并使用列表,但问题是我正在获取一个可以是多个单词的子字符串。

我试过使用 str.partition ,它非常接近于做我想要的,但它只得到第一个关键字。

代码:

text = "Hello World how are you doing Hello is the keyword I'm trying to get Hello is a repeating word"
part = text.partition("Hello")
part = list(map(str.strip, part))

输出:

['', 'Hello', "World how are you doing Hello is the keyword I'm trying to get Hello is a repeating word"]

这正是我需要的第一个关键字。 我有足够的时间来获取前后词。 不幸的是,当我正在寻找的子字符串重复时,这让我失望了。

如果输出可以是列表分区列表,那么我实际上可以让它工作。 我应该如何处理这个?

text = "Hello World how are you doing Hello is the keyword I'm trying to get Hello is a repeating word"

def recursive_partition(text, pattern):
  if not text:
    return text
  tmp = text.partition(pattern)
  if tmp and tmp[1]:
    return [tmp[0]] + [tmp[1]] + recursive_partition(tmp[2], pattern)
  else:
    return [tmp[0]]

res = recursive_partition(text, "Hello")
print(res)  # ['', 'Hello', ' World how are you doing ', 'Hello', " is the keyword I'm trying to get ", 'Hello', ' is a repeating word']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM