简体   繁体   English

我需要在这里使用什么功能?

[英]What function do I need to use here?

So this is for homework but I have the program pretty much entirley worked out I'm just not sure what function i need to use for this last part.所以这是家庭作业,但我的程序几乎完全解决了我只是不确定我需要在最后一部分使用什么功能。

if __name__ == '__main__':
    f = open("flu_data.csv", "r")
    for line in f:
        try:
            line = line.split(",")
            y = line[2]
            if "48" <= y <= "8":
                x = line[10]
                c = (int(x))
                a = line[8]
                b = (int(a)/int(x))
                print("for week:", y , "of year", line[1], "the number of patients with the flu was", c,
                      "the percentage of elderly patients was", b)
                print("the week with the most flu cases was", y , "with" ?)
        except:
            print("problem with:", line)
    f.close()

What I'm trying to do here is get it to print the greatest x value is but I'm not sure which function actually does that when referencing the file.我在这里尝试做的是让它打印最大的 x 值,但我不确定在引用文件时哪个函数实际上是这样做的。 All I need to know is what function do I use here.我只需要知道我在这里使用什么功能。

You can't compute the maximum until you've collected all of the data.在收集所有数据之前,您无法计算最大值。 Once you've done that, use the max function.完成后,使用max函数。

if __name__ == '__main__':
    f = open("flu_data.csv", "r")
    cases_by_week = {}
    for line in f:
        try:
            cols = line.split(",")
            year = cols[1]
            week = cols[2]
            elderly_patients = int(cols[8])
            total_patients = int(cols[10])
            if "48" <= week <= "8":
                cases_by_week[week] = total_patients
                elderly_pct = elderly_patients / total_patients
                print(
                    f"for week: {week} of year {year} the number of "
                    f"patients with the flu was {total_patients} "
                    f"the percentage of elderly patients was {elderly_pct}"
                )
        except Exception as e:
            print(f"skipping line '{line}': {e}")
    f.close()
    max_cases = max(cases_by_week, key=cases_by_week.get)
    print(
        f"the week with the most flu cases was {max_cases} "
        f"with {cases_by_week[max_cases]}"
    )

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

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