【问题标题】:Using a for loop to calculate the mean of a list of tuples in Python在 Python 中使用 for 循环计算元组列表的平均值
【发布时间】:2021-10-02 21:52:08
【问题描述】:

我的任务是使用 for 循环获取整数的均值,即每个县的疫苗接种率,分配给下面列表中元组的第二个索引。我查看了类似的 Q/A 并尝试调整提供的解决方案,但我似乎无法以有效的方式将它们与我的元组列表相关联。

vacc_counties = [
    ("Pulaski", 42.7),
    ("Benton", 41.4),
    ("Fulton", 22.1),
    ("Miller", 9.6),
    ("Mississippi", 29.4),
    ("Scott County", 28.1),
]

我得到的平均值是第二个索引的总和除以长度 sum()/len(),但我不知道如何引用第二个索引,或者如何将变量与整个列表中第二个索引中的数字的实例。我迷失了在这里找到正确的语法来获取总和和长度,并以输出平均值的方式划分它们。

一些 Q/A 提到了导入 pandas 或 numpy.mean,但我一直在弄清楚如何将它们应用到我自己的代码中。

我们将不胜感激。

【问题讨论】:

    标签: python list tuples mean


    【解决方案1】:
    vacc_counties = [
        ("Pulaski", 42.7),
        ("Benton", 41.4),
        ("Fulton", 22.1),
        ("Miller", 9.6),
        ("Mississippi", 29.4),
        ("Scott County", 28.1),
    ]
    
    
    #To get a tuple in the list, in this case the first tuple:
    print(vacc_counties[0])
    
    #To get a value in the tuple, in this case the second value in the second tuple:
    print(vacc_counties[1][1])
    
    
    
    #Apply this to a loop:
    for tup in vacc_counties:
    
        #For every tuple in the list it prints the second value of the tuple
        print(tup[1])
    
    
    #To get the means:
    #Get all percents into list
    percents = []
    for tup in vacc_counties:
        percents.append(tup[1])
    
    #Now add up all the percents to get a total
    total = 0
    for percent in percents:
        total += percent
    #Finally divide the added percents by the length of percents
    print(total/len(percents))
    

    如果您还有其他问题,请在此处提问!

    【讨论】:

    • 感谢您的浏览和分解,这正是我所需要的。你的回答解决了问题,让我在那些给我带来麻烦的事情上占了上风。再次感谢!
    【解决方案2】:

    这里有两种可能的解决方案:

    使用sum() + len()

    vacc_counties = [
        ("Pulaski", 42.7),
        ("Benton", 41.4),
        ("Fulton", 22.1),
        ("Miller", 9.6),
        ("Mississippi", 29.4),
        ("Scott County", 28.1),
    ]
    avg=round(sum(x[1] for x in vacc_counties)/len(vacc_counties),2)
    print(avg)
    

    使用numpy

    import numpy as np
    
    array = np.array(vacc_counties)
    print(array[:,1].astype(float).mean())
    

    【讨论】:

    • 谢谢!这些解决方案有效。我很感激你分享它们。我学到了新东西。再次感谢您。
    • 最欢迎@Psiberian_Mushky 如果它解决了问题,请考虑接受并支持答案
    【解决方案3】:

    在您掌握 Python 的基础知识之前,不要担心使用 pandas 或 numpy。这些库为数据科学家提供了一种更紧凑、更快速的数据处理方式。

    当你循环数据时

    for county_data in vacc_counties:
    

    您可以使用city = county_data[0] 获取元组中的第一项,使用vaccination_rate = county_data[1] 获取第二项

    所以在一起就是

    for county_data in vacc_counties:
        vaccination_rate = county_data[1]
    

    我会留给你,然后找出获得平均值的确切代码,但基本上你需要遵循以下三个步骤:

    1. 在开始循环之前,您需要创建一个变量来跟踪运行总计

    2. 然后在循环中,您需要将每个疫苗接种率添加到运行总数中。

    3. 在将运行总数除以项目总数的循环之后,使用len function 获取vacc_counties 列表中的项目总数。 (元组的总数将与疫苗接种率的总数相同)。


    或者,您确实在问题中提到了sum function。你会认为求平均值是 sum 函数可以派上用场的一个地方,它可以。如果你想使用 sum 函数,你需要一个单独的列表,其中只有数字。 TheRealJake 在创建他的percents 列表时做了什么。创建该列表后,您可以轻松地调用列表中的sum 函数来获取总数。例如total = sum(list_of_numbers).

    【讨论】:

    • 这简直太棒了!非常感谢您的故障和概念演练。我要解决这个问题。这解决了最困扰我的关于解决第二个索引并引用它们的问题。再次感谢!
    猜你喜欢
    • 2018-10-04
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多