Analysis of the new crown epidemic data in Python

Code section

#建立一个省/市的类
class Province :
    def __init__(self,Name,New,Diagnosis,Cured,Dead):#双下划线
            self.Name=Name
            self.New=New
            self.Diagnosis=Diagnosis
            self.Cured=Cured
            self.Dead=Dead
    def input(self):
            self.Name=input()
            self.New,self.Diagnosis,self.Cured,self.Dead=map(int,input().split())
            
#用字典建立多个对象
d={}

def nation():
    print("请录入34个省级行政区的疫情信息:\n省 新增 确诊 治愈 死亡")
    for i in range(34):
        d[i]=Province("省份",0,0,0,0)    
    total=0
    total1=0
    total2=0
    total3=0
    for i in range(34):
        d[i].input()
        total=total+d[i].Diagnosis
        total1=total1+d[i].Cured
        total2=total2+d[i].Dead
        total3=total3+d[i].New
    print("---------------------------------------------------------------")
    print("全国\n新增:",total3,"\n确诊:",total,"\n治愈:",total1,"\n死亡:",total2)
    print("---------------------------------------------------------------")
    print("省\t新增\t确诊\t治愈\t死亡\t占比\t治愈率\t死亡率")
    for i in range(0,34):
        a='{:.2%}'.format(d[i].Diagnosis/total)
        b='{:.2%}'.format(d[i].Cured/d[i].Diagnosis)
        c='{:.2%}'.format(d[i].Dead/d[i].Diagnosis)
        #c=round(d[i].Dead/total2*100,2)
        print(d[i].Name,d[i].New,d[i].Diagnosis,d[i].Cured,d[i].Dead,a,b,c,sep='\t')
    print("---------------------------------------------------------------")
    j=(total1-d[0].Cured)/(total-d[0].Diagnosis)
    k=(total2-d[0].Dead)/(total-d[0].Diagnosis)
    e='{:.2%}'.format((total-d[0].Diagnosis)/total)
    f='{:.2%}'.format(j)
    g='{:.2%}'.format(k)
    print("除湖北",(total3-d[0].New),total-d[0].Diagnosis,total1-d[0].Cured,total2-d[0].Dead,e,f,g,sep='\t')
    
def hubei():
    print("请录入17个市的疫情信息:\n市 新增 确诊 治愈 死亡")
    for i in range(18):
        d[i]=Province("市",0,0,0,0)     
    total=0
    total1=0
    total2=0
    total3=0 
    for i in range(18):
        d[i].input()
        total=total+d[i].Diagnosis
        total1=total1+d[i].Cured
        total2=total2+d[i].Dead
        total3=total3+d[i].New
    print("---------------------------------------------------------------")
    print("湖北\n新增:",total3,"\n确诊:",total,"\n治愈:",total1,"\n死亡:",total2)
    print("---------------------------------------------------------------")
    print("市\t新增\t确诊\t治愈\t死亡\t占比\t治愈率\t死亡率")
    for i in range(0,18):
        a='{:.2%}'.format(d[i].Diagnosis/total)
        b='{:.2%}'.format(d[i].Cured/d[i].Diagnosis)
        c='{:.2%}'.format(d[i].Dead/d[i].Diagnosis)
        #c=round(d[i].Dead/total2*100,2)
        print(d[i].Name,d[i].New,d[i].Diagnosis,d[i].Cured,d[i].Dead,a,b,c,sep='\t')
    print("---------------------------------------------------------------")
    j=(total1-d[0].Cured)/(total-d[0].Diagnosis)
    k=(total2-d[0].Dead)/(total-d[0].Diagnosis)
    e='{:.2%}'.format((total-d[0].Diagnosis)/total)
    f='{:.2%}'.format(j)
    g='{:.2%}'.format(k)
    print("除武汉",(total3-d[0].New),total-d[0].Diagnosis,total1-d[0].Cured,total2-d[0].Dead,e,f,g,sep='\t')
    
#main函数
print("---------------------------------------------------------------")
print("新冠肺炎疫情分析系统\n1)全国分析\n2)湖北分析\n请选择要进行的操作:")
case=input()
print(case)
if case=="1":#双引号  
    nation()
else:
    hubei()

User Interface

Data source and data processing

Source: Baidu - the latest outbreak map real-time data reporting

Data processing: the text in the document tab replaced with spaces, and then "-" is replaced by "0", copy and paste.

Data Display

February 23 data

to sum up

1. do not need to declare the various attributes of the class

2. constructor __init __ () is double underlined, not a single underline.

3.self this pointer corresponds to the C ++ member function of the first argument must be self class

4.Python no structure, the class may be used instead. Difficulty is to create multiple classes of objects.

You can not create it manually one by one, that much trouble. It can be used to store the dictionary.

Dictionary key (key) to store the object name, the dictionary value (value) to store objects.

5. Since Python object reference is passed, outside the total function of the digital type is passed by value can not be changed function

The value of total global variables outside the function, you can switch to local variables inside a function.

6. string double quotes

Different 7.Python for loop and C ++

//Python
for i in range(34)
//C++
for(int i=0;i<34;i++)

8.Python C ++ using indentation instead of braces and semicolons are omitted, pay special attention to the time of writing.

9. brackets too, can be taken out to replace the whole part of the letter.

10. The data output line, each interval data may also be used with spaces Tab, setting method: print () function plus end sep = '\ t' or sep ''.

11. The percentage of output formats:

a='{:.2%}'.format(一个小数)//控制在小数点后两位

12.Python no Switch-Case structure.

13. Fortunately, yesterday brushed up classes and objects in C ++, or have forgotten. However, this program seems to not have to use class to write it, I might write complicated.

Published 115 original articles · won praise 9 · views 8127

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/104470253