简体   繁体   English

从特定列表中的关键字之前从句子中删除单词

[英]Removing words from a sentence before a keyword from a specific list

I have a list of keywords keywords = [",", "or", "and"] and a sentence s = built on the kernel, is written largely in我有一个关键字列表keywords = [",", "or", "and"]和一个句子s = built on the kernel, is written largely in

I want to remove all the words from the above sentence before any keyword from the above list, in this case ",".我想从上面列表中的任何关键字之前删除上面句子中的所有单词,在这种情况下是“,”。 So the Output will be is written largely in.所以 Output 将被is written largely in.

import re

keywords = [",", "or", "and"]
s = "built on the kernel, is written largely in"
split_sentence = re.split(',|or|and',s)[1:]
result = "".join(split_sentence)
print(result)

output: output:

is written largely in

You can use re.split() with pattern as |您可以将re.split()模式一起使用为| joined string of your keywords .加入您的keywords字符串。 It will return you list of strings separated by your keywords.它将返回由关键字分隔的字符串列表。 Since you need the last sub-string from the list , you can access it using -1 as index.由于您需要list的最后一个子字符串,您可以使用-1作为索引来访问它。

For example:例如:

>>> import re
>>> keywords = [",", "or", "and"]
>>> my_str = "Hello, and, or, Stack Overflow"

>>> re.split('|'.join(keywords), my_str)[-1]
' Stack Overflow'

Additionally, if you want to remove additional white spaces from the resultant string, then you can further usestr.strip() on the above string as:此外,如果您想从结果字符串中删除额外的空格,那么您可以进一步在上述字符串上使用str.strip()

>>> re.split('|'.join(keywords), my_str)[-1].strip()
'Stack Overflow'

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

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