简体   繁体   English

如何计算列表中符合特定条件的项目的百分比?

[英]How to calculate percentages of items in a list that meet specific criteria?

I am making a program that looks at a file with data for hail in the US I have successfully created a class with objects (All the states), and have the state abbreviation, number of total hail reports, and list of hail sizes for each state/object. 我正在制作一个程序,该程序在美国查看包含冰雹数据的文件。我已经成功创建了一个包含对象(所有州)的类,并具有状态缩写,冰雹报告总数以及每个冰雹大小的列表状态/对象。

I have to make a table showing statistics of the hail sizes for each state. 我必须制作一张表格,显示每个州的冰雹大小统计数据。 I created a method that averages the hail sizes for each state, but I can't figure out how to make a method that calculates the percentages of hail sizes that fall within a certain size range. 我创建了一种方法来平均每个州的冰雹大小,但我不知道如何制作一种方法来计算落在特定大小范围内的冰雹大小的百分比。

The table has 5 categories, for these hail sizes: <1.25, 1.25-1.99, 2.0-2.49, 2.5-2.99, >3.0. 该表针对这些冰雹大小有5个类别:<1.25、1.25-1.99、2.0-2.49、2.5-2.99,> 3.0。
I am pretty sure I need to do a rolling sum for each category, but I just can't figure out how to do it with so many categories. 我很确定我需要对每个类别进行汇总,但是我只是想不出如何处理这么多个类别。 If I could do a rolling sum, it would just be that sum/ the total number of reports for that state. 如果我可以进行总和,那将是该总和/该州的报告总数。

Here is the code I have so far: 这是我到目前为止的代码:

 class stateHail: def __init__(self, state): self.stateAbbr = state self.hailReports = 0 self.hailSize = [] def avgSize(self): self.avg = (sum(self.hailSize))/self.hailReports #def percents(self): #??? #Creates objects needed for class states = [] for x in ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "WA", "WV", "WI", "WY", "PR", "VA", "VI"]: stateObject = stateHail(x) states.append(stateObject) #Opens file and assigns state abbreviation, hail report number, and list of hail sizes to each object/state fileHail = open("90-99_hail.csv", "r") try: for line in fileHail: splitLine=line.split(",") abbrFile=splitLine[7] for x in states: if (abbrFile==x.stateAbbr): x.hailSize.append(float(splitLine[10])) x.hailReports = x.hailReports + 1 except: fileHail.close() #Calculates average for each state for x in states: if (x.hailReports>100): x.avgSize() print x.avg 

If I understand your question correctly, then you can store the percentages of each range in a list. 如果我正确理解您的问题,则可以将每个范围的百分比存储在列表中。 The code may look something as follows 该代码可能如下所示

    self.percentages = [(self.hailSize[i]/self.hailReports) * 100 for i in range(5)]

If you have a list of hail sizes like this: [1.1,1.1,1.6,1.6,2.1,2.6,3.1,3.1,3.1] 如果您有这样的冰雹尺寸列表: [1.1,1.1,1.6,1.6,2.1,2.6,3.1,3.1,3.1]

you can get a list like this: [2, 2, 1, 1, 3] 您可以得到如下列表: [2, 2, 1, 1, 3]

from this: 由此:

return [len([item for item in self.hailSize if small<=item<big]) for small,big in [(0,1.25), (1.25,2), (2,2.5), (2.5,3), (3,200)]]

By the way, instead of saving a self.hailReports variable to count how many elements there are in self.hailSize , you can just use len(self.hailSize) . 顺便说self.hailSize ,您可以使用len(self.hailSize) ,而不是保存self.hailReports变量来计算self.hailSize有多少个元素。 Next, have avgSize() simply do return sum(self.hailSize)/len(self.hailSize) , and then you don't have to call a function to update a variable all the time - just get the average size with avgSize() . 接下来,让avgSize()简单地return sum(self.hailSize)/len(self.hailSize) ,然后您不必一直调用函数来更新变量-只需使用avgSize()获得平均大小avgSize()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM