python - 输出在字符串中找到的特定单词(从列表中)

标签 python split

我正在尝试识别字符串句子中识别的特定单词(从列表中)。

我已经成功导入了一个(不适当的)单词列表,然后将其与输入句子进行比较,看看该单词是否在句子中(在基本的 if 循环中使用) - 它运行良好(下面的代码),但现在我需要确定实际上发现哪个单词用作输出的一部分。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from inappropriate_words import inappropriate # a list of inappropriate words
import sys

message = ' '.join(sys.argv[1:]) # the input message already converted to lowercase
message = message.replace(".", "") # to remove the full stop as well
#print (message) #to test if needed

if any(word in message.split() for word in inappropriate):
    print "SAMPLE WORD is inappropriate."

一个例子是:
输入:“你喜欢cookie吗”
流程:Cookies 位于不适当的列表中,因此它被识别并触发 if 循环
输出:“Cookie 是不合适的。” # 我喜欢 cookies SBTW

最佳答案

我会使用一个集合来存储不适当的单词,然后简单地进行查找,这是O(1),而不是使用列表O(n):

st = set(inappropriate)
message = ' '.join(sys.argv[1:]) # the input message already converted to lowercase
message = message.replace(".", "") # to remove the full stop as well

for word in message.split():
    if word in st:
        print "{} is inappropriate.".format(word)

如果您想查看是否有任何单词匹配,请添加一个中断,以查看所有匹配的单词按原样使用。

您还可以使用 set.intersection 查找所有常见单词:

comm = st.intersection(message.split()) 

最后,您可以去掉单词中的标点符号并使用 argv[1:] ,而不是加入和替换:

from string import punctuation

from inappropriate_words import inappropriate # a list of     inappropriate words
import sys

for word in sys.argv[1:]:
    if word.strip(punctuation) in st:
        print "{} is inappropriate.".format(word)

关于python - 输出在字符串中找到的特定单词(从列表中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31904098/

相关文章:

r - 在 R 中,如何在不使用分隔符的情况下拆分字符串

python - 从 node.js 调用 Sagemaker Tensorflow Resnet50 端点

python - conda 是否从使用 pip install 安装的 pypi 更新包?

python - SQLAlchemy db.create_all() 错误,未创建数据库

java - 字符串分词

python - 在 split() 操作后获取 pandas 中唯一的字符串列表

python - 如何在标准环境中将文件加载到 Google-App-Engine

python - 在 python pandas 中查找每个日期的字母计数

java - 如何在 Java 中拆分字符串,忽略多个连续的标记

Java,RegEx,通过忽略内部出现将字符串与另一个字符串分割