编程语言
首页 > 编程语言> > Python第五次作业-朱名铨

Python第五次作业-朱名铨

作者:互联网

习题1:读入文件pmi_days.csv,完成以下操作:
1.统计质量等级对应的天数,例如:
优:5天
良:3天
中度污染:2天
2.找出PMI2.5的最大值和最小值,分别指出是哪一天。

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 =="中度污染":
                a=a+1
            if value =="轻度污染":
                b=b+1
            if value =="良":
                c=c+1
            if value =="优":
                d=d+1
            if key == 'PM2.5':
                e = dict.get('PM2.5')
                lista.append(e)
                lista =list(map(int,lista))
            if key == "日期":
                f = dict.get('日期')
                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("中度污染出现次数为:",a)
print("轻度污染出现次数为:",b)
print("良出现次数为:",c)
print("优出现次数为:",d)
print("PM2.5最大为:",max(lista))
print("PM2.5最大那天为",date)
print("PM2.5最小为:",min(lista))
print("PM2.5最小那天为",mindate)

习题2:读入文件1980-2018GDP.csv,完成以下操作:
1.按行输出每年GDP数据,表头列名如文件第1行所示。

2.将各年GDP数据转换成字典格式,以年份为keys,其它值为values(数据类型为列表方式),例如:
{
2017:[827121.7,6.8%,60989]
........
}

3.遍历字典数据,求出GDP的最小值与最大值,并输出数据与对应的年份。

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(亿元)':
                bijiao.append(float(dict.get('GDP(亿元)')))
        dict.get('年份')
        listb.append(int(dict.get('年份')))
    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最大值为:{}亿元 那一年是:{}年".format(max(bijiao),maxyear))
print("GDP最小值为:{}亿元 那一年是:{}年".format(min(bijiao),minyear))

标签:Python,朱名铨,第五次,dict,reader,print,csv,lista,listb
来源: https://www.cnblogs.com/luckyvip520/p/10920319.html