简体   繁体   中英

Appending string print output

I want to find word in sentence to give a category to sentence. For that I have created a function listed below:

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)

the output is :

None
None
AC problem
None
AC problem

How should I save this output to a variable

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM