【问题标题】:Calculate the average of the listed elements ine the list [duplicate]计算列表中所列元素的平均值[重复]
【发布时间】:2018-06-21 07:53:34
【问题描述】:

我有两个列表:

  1. 与元素:[0.7181,7.3,561257621.7,0.12122,9.8]
  2. 带索引:[0,2]

如何计算我在第二个表中拥有的索引的元素的平均值?

【问题讨论】:

  • 你有什么尝试吗?
  • 所以我可以创建附加表并将其附加到具有加工索引的项目中。但我正在寻找更快的方法。
  • numpy.mean(numpy.array(elements)[indices])

标签: python numpy


【解决方案1】:

我会使用:

>>> elements = [0.7181, 7.3, 561257621.7, 0.12122, 9.8]
>>> indices = [0, 2]
>>> sum([elements[i] for i in indices])/len(indices)
280628811.20905

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2021-06-03
    • 2018-03-19
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多