Python homework 5 - Zhu Mingquan

Exercise 1: read in the file pmi_days.csv, and complete the following operations:1. Count the days corresponding to the quality level, for example:Exc...

Exercise 1: read in the file pmi_days.csv, and complete the following operations:
1. Count the days corresponding to the quality level, for example:
Excellent: 5 days
Liang: 3 days
Moderate pollution: 2 days
2. Find out the maximum and minimum value of PMI2.5, and indicate which day it is respectively.

import csv a=0 b=0 c=0 d=0 lista=[] listb=[] with open("C:/Users/Administrator/pmi_days.csv",'r') as f: reader = csv.reader(f) fieldnames = next(reader) csv_reader = csv.DictReader(f,fieldnames=fieldnames) for row in csv_reader: dict={} for key,value in row.items(): dict[key]=value if value =="Moderate pollution": a=a+1 if value =="Mild pollution": b=b+1 if value =="good": c=c+1 if value =="excellent": d=d+1 if key == 'PM2.5': e = dict.get('PM2.5') lista.append(e) lista =list(map(int,lista)) if key == "date": f = dict.get('date') listb.append(f) for i in range(0,len(lista)): if lista[i] == max(lista): date = listb[i] if lista[i] == min(lista): mindate = listb[i] print("The frequency of moderate pollution is:",a) print("The frequency of light pollution is:",b) print("Good times:",c) print("The optimal frequency is:",d) print("PM2.5 Maximum is:",max(lista)) print("PM2.5 The biggest day is",date) print("PM2.5 Minimum is:",min(lista)) print("PM2.5 The youngest day was",mindate)

Exercise 2: read in the file 1980-2018GDP.csv, and complete the following operations:
1. Output annual GDP data by line, and the header column name is shown in line 1 of the document.

2. Convert the GDP data of each year into a dictionary format, with the year as the keys and other values as the values (data type is list mode), for example:
{
2017:[827121.7,6.8%,60989]
........
}

3. Traverse the dictionary data, find the minimum and maximum value of GDP, and output the data and the corresponding year.

import csv lista=[] listb=[] bijiao=[] x={} with open("C:/Users/Administrator/1980-2018GDP.csv", 'r') as f: reader = csv.reader(f) fieldnames = next(reader) print(fieldnames) for row in reader: lista.append(row) for i in range(0,len(lista)): print("%-8s"%lista[i][0],"\t%-10.7s"%lista[i][1],"\t%-12.5s"%lista[i][2],"\t%-13.5s"%lista[i][3],lista[i][4]) with open("1980-2018GDP.csv", 'r') as f: reader = csv.reader(f) fieldnames = next(reader) csv_reader = csv.DictReader(f, fieldnames=fieldnames) for w in csv_reader: dict = {} for key, value in w.items(): dict[key] = value if key=='GDP(Billion yuan)': bijiao.append(float(dict.get('GDP(Billion yuan)'))) dict.get('Particular year') listb.append(int(dict.get('Particular year'))) for i in range(0,len(lista)): x[listb[i]]=lista[i][1:] if lista[i] == max(lista): maxyear = listb[i] if lista[i] == min(lista): minyear = listb[i] print(x) print("GDP The maximum value is:{}That year is:{}year".format(max(bijiao),maxyear)) print("GDP The minimum value is:{}That year is:{}year".format(min(bijiao),minyear))

5 November 2019, 10:34 | Views: 9982

Add new comment

For adding a comment, please log in
or create account

0 comments