Python 不删除单词,但适用于单个字母

发布于 2025-01-16 17:14:43 字数 705 浏览 0 评论 0原文

我有一个项目,我应该使用下面的代码从语句中删除一些单词:

forbidden_words = ["And","is","to","from","the"]
f = "And thus brought us to this conclusion from the existing inference"

h = []
for i in f:
    if i not in forbidden_words:
        h.append(i)
"".join(h)

输出:'因此从现有的推论中得出了这个结论'

预期输出:'因此给我们带来了这个结论现有的推论'

我不知道为什么,但它适用于单个字母而不是单个单词,假设下面的代码:

forbidden_words = ["u","t","c","b","a"]
f = "And thus brought us to this conclusion from the existing inference"

h = []
for i in f:
    if i not in forbidden_words:
        h.append(i)
"".join(h)

输出:'and hs rogh so his onlsion from he exising推断'

为什么会发生这种情况?

I have this project where I'm supposed to remove some word from a statement using code below:

forbidden_words = ["And","is","to","from","the"]
f = "And thus brought us to this conclusion from the existing inference"

h = []
for i in f:
    if i not in forbidden_words:
        h.append(i)
"".join(h)

Output: 'And thus brought us to this conclusion from the existing inference'

Expected output: 'thus brought us this conclusion existing inference'

I'm not sure why but it works well for single letter but not a single word, suppose the code below:

forbidden_words = ["u","t","c","b","a"]
f = "And thus brought us to this conclusion from the existing inference"

h = []
for i in f:
    if i not in forbidden_words:
        h.append(i)
"".join(h)

Output: 'And hs rogh s o his onlsion from he exising inferene'

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下凄凉 2025-01-23 17:14:43

您需要在空格上拆分 f 以便迭代单词:

h = []
for word in f.split():
    if word not in forbidden_words:
        h.append(word)
" ".join(h)

或者,使用生成器表达式:

" ".join(word for word in f.split() if word not in forbidden_words)

You need to split f on spaces in order to iterate over the words:

h = []
for word in f.split():
    if word not in forbidden_words:
        h.append(word)
" ".join(h)

Or, with a generator expression:

" ".join(word for word in f.split() if word not in forbidden_words)
拧巴小姐 2025-01-23 17:14:43
import re
forbidden_words = ["And","is","to","from","the"]
f = "And thus brought us to this conclusion from the existing inference"
for words in forbidden_words:
     f = ''.join(f.split(words))
f = re.sub(' +', ' ', f)
import re
forbidden_words = ["And","is","to","from","the"]
f = "And thus brought us to this conclusion from the existing inference"
for words in forbidden_words:
     f = ''.join(f.split(words))
f = re.sub(' +', ' ', f)
樱桃奶球 2025-01-23 17:14:43

您必须迭代不在字符串上的字符串列表,如下所示。

forbidden_words = ["And","is","to","from","the"]
sentence = "And thus brought us to this conclusion from the existing inference".split()

filt_text = [ w  for w in sentence if w not in forbidden_words]

filt_sentence = ' '.join(filt_text)

print(filt_sentence )

You have to iterate over a list of strings not on the string as shown below.

forbidden_words = ["And","is","to","from","the"]
sentence = "And thus brought us to this conclusion from the existing inference".split()

filt_text = [ w  for w in sentence if w not in forbidden_words]

filt_sentence = ' '.join(filt_text)

print(filt_sentence )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文