【问题标题】:Sum elements of list ignoring NaN without numpy在没有 numpy 的情况下忽略 NaN 的列表元素求和
【发布时间】:2023-02-18 00:07:26
【问题描述】:

我不能使用 numpy,如何模拟 .nansum 方法?例子

x = [1, NaN, 2, 10]

【问题讨论】:

    标签: python list sum


    【解决方案1】:

    您可以将生成器表达式传递给sum

    from math import isnan
    print(sum(e for e in x if not isnan(e)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-25
      • 2023-02-08
      • 1970-01-01
      • 2010-10-12
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      相关资源
      最近更新 更多