Добавление вывода на печать строки - PullRequest
0 голосов
/ 22 сентября 2018

Я хочу найти слово в предложении, чтобы дать категорию предложению.Для этого я создал функцию, указанную ниже:

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

Как сохранить этот вывод в переменной

Ответы [ 2 ]

0 голосов
/ 22 сентября 2018
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)
0 голосов
/ 22 сентября 2018
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...