附加字符串打印输出

时间:2018-09-22 10:19:43

标签: python string function

我想在句子中找到单词以给句子分类。为此,我创建了下面列出的函数:

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 :(得分: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

答案 1 :(得分:0)

$s1 = sizeof($list1);
$s2 = sizeof($list2);

if ($s2 < $s1) {
    $k = $s2;
} else {
    $k = $s1;
}

foreach ($list1 as $k => $v) {

    $words['output'][] = [
        'id' => $k,
        'value' => $v . '.' . $list2[$k]
    ];
}