根据句子中的单词将句子分类为多个句子

时间:2019-06-19 11:09:15

标签: python list

data = ["my web portal is not working","online is better than offline", "i like going to pharmacy shop for medicines"]
words = ["web", "online"]

我想遍历句子并检查列表词中是否有任何词。如果是,我想为每个句子指定一个类别,否则为“其他”类别。如果我要从单词列表中给出单个单词,这是可行的,但是我想一次检查所有单词。

b = []
def ch_1(x,y):
    for i in x:
        if y in i:
            b.append("web")
        else:
            b.append("others")
    return b

出错:

  

in'中需要字符串作为左操作数,而不是列表

5 个答案:

答案 0 :(得分:0)

您需要遍历作为参数给出的两个列表。

def ch_1(x,y):
    b = []
    for i in x:
        for j in y:
            if j in i:
                b.append('web')
                break
        else:
            b.append('others')
    return b
print(ch_1(data, words))

输出

['web', 'web', 'others']

答案 1 :(得分:0)

使用in operator检查字符串“包含”子字符串。

data = ["my web portal is not working","online is better than offline", "i like going to pharmacy shop for medicines"]
words = ["web", "online"]

def ch_1(x,y):
    b = []
    for i in x:
        web = False
        for j in y:
            if j in i:
                web = True
                break
        if web:
            b.append("web")
        else:
            b.append("others")
    return b

print(ch_1(data,words))

O / P:

['web', 'web', 'others']

答案 2 :(得分:0)

此代码适用于words中的任何单词和data中的句子:

data = [
    "my web portal is not working",
    "online is better than offline",
    "i like going to pharmacy shop for medicines"
]

words = ["web", "online"]


def ch_1(words, data):
    categories = {sentence: [] for sentence in data}
    for sentence in data:
        for word in words:
            if word in sentence:  # and categories[sentence] == [] ((if you want exactly one category for each sentence))
                categories[sentence].append(word)
    for sentence in categories:
        if categories[sentence] == []:
            categories[sentence].append('others')
    return categories

print(ch_1(words, data))
{
'i like going to pharmacy shop for medicines': ['others'],
'online is better than offline': ['online'],
'my web portal is not working': ['web']
}

答案 3 :(得分:0)

在您的声明中

if y in i:

y是一个列表。您没有显示如何调用ch_1,但我假设您使用的是ch_1(数据,单词)。因此,参数y为[“ web”,“ online”],并且您试图在i中找到一个字符串的完整列表。这样您就会收到消息

TypeError: 'in <string>' requires string as left operand, not list

因为它期望y是要在字符串i中找到的字符串。向它提供列表没有意义。如果您在i中使用y [0]或在i中使用y [1],那么您将正确地提供要在i中找到的字符串。

答案 4 :(得分:0)

尝试[conditional expression for sentence in data]的形式:

data = [
    "my web portal is not working",
    "online is better than offline",
    "i like going to pharmacy shop for medicines",
]
words = ["web", "online"]

["web" if any(word in sentence for word in words) else "others" for sentence in data]