如何删除 python 中字符串的特定部分

how to remove specific parts of string in python

我试图删除具有相同模式的特定单词,这些单词旁边是特定的相同单词。

doc = ["super man good weather", "bet man nice car", "iron man awesome soup"]

我想删除 'super man''bet man''iron man'。这些字符串具有相同的单词 'man',我想同时删除同一个单词 'man' 前面的单词。

我试过了,但是失败了。

for string in doc:
    prep = re.sub('.* man =', '', string)

这不是优雅的做法。但达到目的。

doc = ["super man good weather", "bet man nice car", "iron man awesome soup", "a manned mission to mars"]

keyword = " man " # to make sure that you don't remove words that contain man as substring

doc = [string.split(keyword)[1].strip() if keyword in string else string for string in doc]

print(doc)

输出

['good weather', 'nice car', 'awesome soup', 'a manned mission to mars']

查看实际效果 here

基于正则表达式的解决方案

import re
doc = ["man super man good weather", "a bet man nice car", "iron man awesome man soup", "a manned mission to mars"]
doc = [re.sub('\w+ man ', '', string).strip() for string in doc]
print(doc)

输出

['man good weather', 'a nice car', 'soup', 'a manned mission to mars']

查看实际效果 here

试试这个..应该使用 re

[re.sub('[a-zA-Z]+\s{1}man', '', txt).strip() for txt in doc]

我的方法是

re.sub('\w+ man ', '', t)