【问题标题】:calculating the mean of numeric values in a list of tuples计算元组列表中数值的平均值
【发布时间】:2023-03-24 08:55:02
【问题描述】:

我是 Python 新手,偶然发现了以下问题:我有两个由元组 ('str', float) 组成的列表(listA 和 listB),我必须计算每个列表的浮点值的平均值。

groups = ['A', 'B', 'B', 'A', 'B', 'B', 'B', 'B', 'A', 'A']
scores = [0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3]

list_groups_scores = list(zip(groups,scores))
list_groups_scores.sort()
    
print(list_groups_scores)


listA = list_groups_scores[0:4]
listB = list_groups_scores[5:9]
print(listA, listB)

有人可以帮忙吗? 非常感谢!

【问题讨论】:

  • 在组数组中循环并对当前组的值求和(使用字典保存结果和值的数量。然后对于字典中的每个条目,将总和除以条目数。

标签: python list tuples mean


【解决方案1】:

使用字典来保存分组和分数

from collections import defaultdict

groups = ['A', 'B', 'B', 'A', 'B', 'B', 'B', 'B', 'A', 'A','K']
scores = [0.2, 0.3, 0.9, 1.1, 2.2, 2.9, 0.0, 0.7, 1.3, 0.3,1]
data = defaultdict(list)
for g,s in zip(groups,scores):
  data[g].append(s)
for grp,scores in data.items():
  print(f'{grp} --> {sum(scores)/len(scores)}')

输出

A --> 0.725
B --> 1.1666666666666667
K --> 1.0

【讨论】:

    【解决方案2】:

    试试下面的代码:

    sum = 0
    ii=0
    for sub in listA:
        ii+=1
        i=sub[1]
        sum = sum + i
    avg=sum/ii
    

    【讨论】:

      【解决方案3】:
      s = lambda x:[i[1] for i in x]
      
      print(sum(s(listA))/len(listA))
      
      print(sum(s(listB))/len(listB))
      

      【讨论】:

        【解决方案4】:

        python中有一种特殊类型的循环,看起来像这样。

        listANumbers = [i[1] for i in listA]
        

        它遍历 listA 中的每个元素,并将每个元素 [1] 设置为一个新列表。

        结果:

        [0.2, 0.3, 1.1, 1.3]
        

        然后您可以对新列表求和和取平均值。

        listANumbers = [i[1] for i in listA]
        meanA = sum(listANumbers) / len(listANumbers)
        

        【讨论】:

          【解决方案5】:

          两个不同的点,每个点都有几个解决方案。

          我。如何从列表中的元组中取回浮点数

          • 使用解包成语和列表推导

            float_list = [v for i, v in listA]
            
          • 纯 Python

            float_list = []
            for item in listA:
                float_list.append(item[1])
            

          二。如何计算平均值

          • 从某个模块导入mean

            • from Numpy import mean
            • from statistics import mean
          • 使用sumlength

            对列表中的值求和并除以列表长度

            mean = sum(v in a_list) / len(a_list)
            
          • 纯 Python

            循环聚合值并计数到长度

            aggregate = 0
            length = 0
            for item in a_list:
                 aggregate += item
                 length += 1
            mean = aggregate / length    
            

            注意:选择aggregate 有两个原因:

            - avoiding the use of a built-in function
            - generalizing the name of the variable, for example, a function other than addition could be used.
            

          解决方案

          解决方案可以是步骤 I 和步骤 II 的混合,但临时解决方案可以省去中间列表的构建(在第一部分的示例中为 float_list

          例如:

          • 使用sumlength

            对列表中未打包的值求和并除以列表长度

            mean = sum(v in float_list) / len(float_list)
            

            注意:v in float_list 不会创建列表;它是一个 generator 按需提供浮动。

          • 纯 Python

            循环聚合值并计数到长度

            aggregate = 0
            length = 0
            for item in  listA:
                 aggregate += item[1]
                 length += 1
            mean = aggregate / length 
            

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-06-03
            • 2014-01-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多