【问题标题】:TypeError: unsupported operand type(s) for +: 'NoneType' and 'float' in Python 3.6TypeError:+ 的不支持的操作数类型:Python 3.6 中的“NoneType”和“float”
【发布时间】:2018-06-27 06:37:55
【问题描述】:
avg = moving_average(Y, window_size=12).tolist()
residual = Y - avg
std = np.std(residual)
sigma=3

print({'standard_deviation': round(std, 3),
        'anomalies_dict': collections.OrderedDict([(index, y_i) for
                                                   index, y_i, avg_i in zip_longest(count(), Y, avg)
          if (y_i > avg_i + (sigma*std)) | (y_i < avg_i - (sigma*std))])})

Y --> 一系列浮动元素

运行上述代码后,它在 Python 2.7 上运行良好,但在 3.6 上运行失败。 上述代码与 2.7 版本代码的唯一变化是,我在这里使用 zip_longest 而我在 2.7 代码中使用 izip。

Traceback (most recent call last):

  File "<ipython-input-37-acf40bad87e9>", line 8, in <module>
    index, y_i, avg_i in zip_longest(count(), Y, avg)

  File "<ipython-input-37-acf40bad87e9>", line 9, in <listcomp>
    if (y_i > avg_i + (sigma*std)) | (y_i < avg_i - (sigma*std))])})

TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

在这方面需要帮助。

【问题讨论】:

标签: python types


【解决方案1】:

您可以使用 zip() 函数,只需将 izip 替换为 zip,代码应该可以工作。

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2020-11-10
    • 2017-06-06
    • 2014-03-28
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多