追加字符串打印输出

2024-09-22 16:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在句子中找到一个词来给句子分类。为此,我创建了以下函数:

def theme(x):
    category = ()
    for i in x:
        if 'AC' in i:
            category = 'AC problem'
        elif 'insects' in i:
            category = 'Cleanliness'
        elif 'clean' in i:
            category = 'Cleanliness'
        elif 'food' in i:
            category = 'Food Problem'
        elif 'delay' in i:
            category = 'Train Delayed'
        else:
            category = 'None'
        print(category)

输出为:

None
None
AC problem
None
AC problem

如何将此输出保存到变量


Tags: 函数innoneforifdef分类theme
2条回答
def theme(x):
    output =[]
    category = ()
    for i in x:
        if 'AC' in i:
            category = 'AC problem'
        elif 'insects' in i:
            category = 'Cleanliness'
        elif 'clean' in i:
            category = 'Cleanliness'
        elif 'food' in i:
            category = 'Food Problem'
        elif 'delay' in i:
            category = 'Train Delayed'
        else:
            category = 'None'
        output.append(category)
    return output
def theme(x):

    category = []

    for i in x:
        if 'AC' in i:
            category.append('AC problem')
        elif 'insects' in i:
            category.append('Cleanliness')
        elif 'clean' in i:
            category.append('Cleanliness')
        elif 'food' in i:
            category.append('Food Problem')
        elif 'delay' in i:
            category.append('Train Delayed')
        else:
            category.append('None')

    return category

categories = theme(['bla bla AC bla','bla insects bla'])
print(categories)

相关问题 更多 >