社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  balu  »  全部回复
回复总数  1
3 年前
回复了 balu 创建的主题 » Python不会删除单词,但适用于单个字母

您必须遍历字符串列表,而不是如下所示的字符串。

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 )