0

如何修改此代码,以便当 senti_avg 不可整除(0/值)时,reducer() 输出 NULL 或 NONE 而不是崩溃?

def reducer(self, bs_id, value):
    avg_data = list(value)
    senti_sum = sum([a[0] for a in avg_data])
    word_sum = sum([a[1] for a in avg_data])
    senti_avg = senti_sum/float(word_sum)

    yield (bs_id, senti_avg)
4

1 回答 1

4

您使用python 异常处理

try:
    senti_avg = senti_sum/float(word_sum)
except ZeroDivisionError:
    senti_avg = None
于 2013-04-09T14:08:56.350 回答