使用 for 循环计算 Python 中元组列表的平均值
Using a for loop to calculate the mean of a list of tuples in Python
我的任务是使用 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,但我一直在弄清楚如何将它们应用到我自己的代码中。
我们将不胜感激任何帮助。
这里有 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())
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))
如果您还有其他问题,请在此处提出!
在掌握 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]
我会把它留给你,然后找出获得平均值的确切代码,但基本上你需要遵循以下三个步骤:
在开始循环之前,您需要创建一个变量来跟踪 运行 总数
然后在循环内,您需要将每个疫苗接种率添加到 运行 总数。
循环后,当需要将 运行 总数除以项目总数时,使用 len function 获取项目总数 vacc_counties
列表。 (元组总数将与疫苗接种率总数相同)。
或者,您确实在问题中提到了 sum function。您可能会认为求平均值是 sum 函数可以派上用场的一个地方,它确实可以。如果你想使用 sum 函数,你需要一个单独的列表,其中只有数字。 theRealJake 在创建他的 percents
列表时做了什么。创建该列表后,您可以轻松调用列表上的 sum
函数来获取总数。例如total = sum(list_of_numbers)
.
我的任务是使用 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,但我一直在弄清楚如何将它们应用到我自己的代码中。
我们将不胜感激任何帮助。
这里有 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())
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))
如果您还有其他问题,请在此处提出!
在掌握 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]
我会把它留给你,然后找出获得平均值的确切代码,但基本上你需要遵循以下三个步骤:
在开始循环之前,您需要创建一个变量来跟踪 运行 总数
然后在循环内,您需要将每个疫苗接种率添加到 运行 总数。
循环后,当需要将 运行 总数除以项目总数时,使用 len function 获取项目总数
vacc_counties
列表。 (元组总数将与疫苗接种率总数相同)。
或者,您确实在问题中提到了 sum function。您可能会认为求平均值是 sum 函数可以派上用场的一个地方,它确实可以。如果你想使用 sum 函数,你需要一个单独的列表,其中只有数字。 theRealJake 在创建他的 percents
列表时做了什么。创建该列表后,您可以轻松调用列表上的 sum
函数来获取总数。例如total = sum(list_of_numbers)
.