【发布时间】:2019-04-11 14:31:30
【问题描述】:
我需要帮助循环遍历句子/字符串列表,并根据另一个单词列表向前擦除字符串字符。
sentences = ['im not george smith my name is lucas mangulu thank you',
'how shall i call you george smith oh okay got it'
'we have detected a miyagi chung in the traffic flow']
words = ['lucas mangulu', 'george smith', 'miyagi chung']
我知道我必须为 sentences 列表中的每个元素循环。但是后来我被困在如何将 words 列表中的相同元素中的 find() 放入 sentences 列表中。这样最终的结果应该是:
sentences = ['im not george smith my name is',
'how shall i call you'
'we have detected a']
#OR
sentences = ['im not george smith my name is lucas mangulu',
'how shall i call you george smith'
'we have detected a miyagi chung']
【问题讨论】:
-
回答如何找句子的问题,在python中
'my name is lucas mangulu thank you'.find('lucas mangulu')会返回11,也就是'lucas mangulu'在字符串中的位置。从那里您可以使用子字符串操作来提取您需要的内容。 -
您的示例输出混乱且令人困惑。在您的第一个输出中:
['im not george smith my name is',您离开了george smith,但在其他输出中您删除了所有名称。为什么?
标签: python-3.x string list for-loop