Python第五次作业——檀健勇
作者:互联网
习题1:读入文件pmi_days.csv,完成以下操作:
1.统计质量等级对应的天数,例如:
优:5天
良:3天
中度污染:2天
2.找出PMI2.5的最大值和最小值,分别指出是哪一天。
import csv
you=0
liang=0
zhong=0
list=[]
list1=[]
with open("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=='优':
you=you+1
if value=='良':
liang=liang+1
if value=='中度污染':
zhong=zhong+1
if value=='中度污染':
zhong=zhong+1
if key=='PM2.5':
list.append(int(dict.get("PM2.5")))
list1.append(dict.get("日期"))
for i in range(0,len(list)):
if list[i]==max(list):
d=list1[i]
if list[i] == min(list):
x = list1[i]
print("优:{}天".format(y))
print("良:{}天".format(l))
print("中度污染:{}天".format(z))
print("PM2.5最大值:{} 日期:{}".format(max(list),d))
print("PM2.5最小值:{} 日期:{}".format(min(list),x))
习题2:读入文件1980-2018GDP.csv,完成以下操作:
1.按行输出每年GDP数据,表头列名如文件第1行所示。
2.将各年GDP数据转换成字典格式,以年份为keys,其它值为values(数据类型为列表方式),例如:
{
2017:[827121.7,6.8%,60989]
........
}
3.遍历字典数据,求出GDP的最小值与最大值,并输出数据与对应的年份。
import csv
list=[]
list1=[]
zidian={}
daxiao=[]
d=0
e=0
with open("1980-2018GDP.csv",'r') as f:
reader = csv.reader(f)
fieldnames = next(reader)
print(fieldnames)
csv_reader = csv.DictReader(f,fieldnames=fieldnames)
for row in reader:
list.append(row)
for i in range(0,len(list)):
print("%-8s"%list[i][0],"\t%-10.7s"%list[i][1],"\t%-12.5s"%list[i][2],"\t%-13.5s"%list[i][3],list[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 row in csv_reader:
dict={}
for key,value in row.items():
dict[key] = value
if key == 'GDP(亿元)':
daxiao.append(float(dict.get('GDP(亿元)')))
dict.get('年份')
list1.append(int(dict.get('年份')))
for i in range(0, len(list)):
zidian[list1[i]] = list[i][1:]
print(zidian)
for i in range(0,len(daxiao)):
if daxiao[i]==max(daxiao):
d=list1[i]
if daxiao[i]==min(daxiao):
e=list1[i]
print("PM2.5最大值:{} 日期:{}".format(max(daxiao), d))
print("PM2.5最小值:{} 日期:{}".format(min(daxiao), e))
标签:Python,檀健勇,list,fieldnames,第五次,dict,reader,print,csv 来源: https://www.cnblogs.com/tantan19971106/p/10919866.html