Python:如何按短语从右到左拆分字符串(仅限第一次出现)



是否可以在Python 3中从右到左拆分短语(可能不止一个单词(上的字符串(仅限第一次出现(?

目前,我可以根据短语列表拆分字符串,但我有一个优势,即如果字符串中出现多个指定短语,则会同时拆分这两个短语。

问题

给定包含以下内容的CSV样本:

发送
这是三个句子 1
这是第二句 2
我包含一个但也包含两个 3

我已经回答了这个问题,使用string.rfind()从字符串的末尾开始搜索,并迭代可能的短语列表。也许有更好的方法可以做到这一点,不需要迭代,但这是我发现的最好的方法。

one = "THIS IS SENTENCE THREE"
two = "THIS IS SENTENCE TWO"
three = "I CONTAIN ONE BUT ALSO TWO"
four = "I CONTAIN ONE BUT ALSO TWO AND SOME MORE TEXT"
phrases = ['ONE', 'TWO', 'THREE']
def find_words(phrases, string):
i = -1
p = ""
for phrase in phrases:
newI = string.rfind(phrase)
if newI > i:
i = newI
p = phrase
return (string[:i], string[i:i+len(p)], string[i+len(p)::])
print(find_words(phrases, one))
print(find_words(phrases, two))
print(find_words(phrases, three))
print(find_words(phrases, four))

输出:

('THIS IS SENTENCE ', 'THREE', '')
('THIS IS SENTENCE ', 'TWO', '')
('I CONTAIN ONE BUT ALSO ', 'TWO', '')
('I CONTAIN ONE BUT ALSO ', 'TWO', ' AND SOME MORE TEXT')

我相信如果您使用"rsplit(("而不是";split((">

我认为,关键是将其拆分为单词,然后反转列表,然后搜索所有命中数并选择最低的数字:

def split_word(string):
words = ['ONE', 'TWO', 'THREE']
search = string.split()
rsearch = list(reversed(search))
locs = [rsearch.index(w) for w in words if w in rsearch]
if not locs:
return None
target = len(search) - min(locs) - 1
return ' '.join(search[0:target]), search[target], ' '.join(search[target+1:])
print(split_word("THIS IS SENTENCE THREE"))
print(split_word("THIS IS SENTENCE TWO"))
print(split_word("I CONTAIN ONE BUT ALSO TWO"))

输出:

('THIS IS SENTENCE', 'THREE', '')
('THIS IS SENTENCE', 'TWO', '')
('I CONTAIN ONE BUT ALSO', 'TWO', '')

最新更新