将平均值从csv文件中获取到列表中

时间:2022-09-27 22:56:13
import matplotlib
import csv

the csv file below is of many years and tons of DBH

f = open("Species.csv")

reader = csv.reader(f)

------Initializations------

y1974 = []

y1983 = []

y1987 = []

y1989 = []

y1994 = []

y1999 = []

y2004 = []

y2009 = []

y2014 = []


dbhByYear = [y1974, y1983, y1987, y1989, y1994, y1999, y2004, y2009, y2014]



def average(alist):
    sum = 0
    for item in alist:
        sum += float(item)
    avg = sum / len(alist)
    return avg

this allows the user to input a specific species of tree to look at

species = input("Enter abbreviation for species you would like to analyze: ")

this removes the "999" within the file

for row in reader:
    if row[4] == species:
        if row[6]!='999':
            y1974.append(row[6])
        if row[7] != '999':
            y1983.append(row[7])
        if row[9] != '999':
            y1987.append(row[9])
        if row[11] != '999':
           y1989.append(row[11])
        if row[13] != '999':
           y1994.append(row[13])
        if row[15] != '999':
           y1999.append(row[15])
        if row[18]!= '999':
           y2004.append(row[18])
        if row[20] != '999':
           y2009.append(row[20])
        if row[23] != '999':
           y2014.append(row[23])

these print the average DBH for each year

 print(average(y1989))
 print(average(y1994))

This is the function i am using to get the dictionary

 averages = {}
 averages["y1989"]=average

My question is is how do you Create a dictionary with the years as keys and average DBH as values.

我的问题是如何创建一个字典,其中年份为键,平均DBH为值。

If anyone is confused (which i bet people will be as to what we are trying to do) please let me know and i will try and clear up questions you may have.

如果有人感到困惑(我打赌人们会对我们要做的事情做什么)请告诉我,我会尽力解决你可能遇到的问题。

1 个解决方案

#1


0  

Okay! Your last edit showed me exactly where you went wrong! average is a function, so assigning it to a dictionary key does not apply that function to that dictionary key, but, in essence, "renames" the function (this glosses over a bit, but it's not relevant to your question).

好的!您的上一次编辑向我展示了您错误的位置! average是一个函数,因此将它分配给字典键不会将该函数应用于该字典键,但实际上,“重命名”该函数(这会稍微掩盖,但它与您的问题无关)。

So, to make it work the way you're expecting, you would need to change that last line to this:

因此,为了使其按照您期望的方式工作,您需要将最后一行更改为:

averages["y1989"]=average(y1989)

just like you did in the print statement above it.

就像你在上面的print语句中所做的那样。

#1


0  

Okay! Your last edit showed me exactly where you went wrong! average is a function, so assigning it to a dictionary key does not apply that function to that dictionary key, but, in essence, "renames" the function (this glosses over a bit, but it's not relevant to your question).

好的!您的上一次编辑向我展示了您错误的位置! average是一个函数,因此将它分配给字典键不会将该函数应用于该字典键,但实际上,“重命名”该函数(这会稍微掩盖,但它与您的问题无关)。

So, to make it work the way you're expecting, you would need to change that last line to this:

因此,为了使其按照您期望的方式工作,您需要将最后一行更改为:

averages["y1989"]=average(y1989)

just like you did in the print statement above it.

就像你在上面的print语句中所做的那样。