Python数据类型结构( 四 )


字典的多级结构
字典-多级结构-离婚率和结婚率.
#读入数据rFile=open("E:\\编程\\课堂老师程序\\20191106\\Marriage2007_2018.txt","r",encoding="utf-8")content=rFile.read()rFile.close()#输出内容看一看#print(content)#按行切分dataLines=content.splitlines()print(dataLines)hunDict={} #结婚的数据!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!for everyLine in dataLines:tmpData=http://www.kingceram.com/post/everyLine.split(",")Sheng=tmpData[0]dataDict={}#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!print(Sheng)for Year in range(2007,2019):dataLine=tmpData[Year-2007+1]dataLine=dataLine.split("_")onData=http://www.kingceram.com/post/int(dataLine[0])offData=int(dataLine[1])print(Year,onData,offData)dataDict[Year]=(onData,offData)hunDict[Sheng]=dataDictprint(hunDict)wFile=open("D:\marriage.txt","w",encoding="utf-8")firstLine=","for Year in range(2007,2019):firstLine+=str(Year)+"年,"print(firstLine[:-1],file=wFile)for everySheng in hunDict:lineData=http://www.kingceram.com/post/everySheng+","for everyYear in range(2007,2019):onData=http://www.kingceram.com/post/hunDict[everySheng][everyYear][0]offData=hunDict[everySheng][everyYear][1]lineData+=str(round(offData/onData,4)*100)+"%"+","print(lineData[:-1],file=wFile)wFile.close()#eof
利用字典处理50万人名
#打开文件的函数 。def Open():file=open('50万人名.txt','r',encoding='utf-8')mydata=http://www.kingceram.com/post/file.read()file.close()datalist=mydata.splitlines()mylist=[]#姓名的列表for everyStr in datalist:newStr=list(everyStr)mylist.append(newStr)#将姓和名分开 。xinglist=[]#姓的列表 。姓一个一个放到列表minglist=[]for everylist in mylist:xinglist.append(everylist[0])minglist.append(everylist[1:])return xinglist,minglist'''#第一和第二函数,可以合并直接在字典中解决 。xingDic={}for ilist in mylist:xing=ilist[0]xingDict[xing]=xingDict.get(xing,0)+1#如此步骤更少 。namelen=len(ilist)if namelen==3:if ilist[1]==ilist[2]:repeatStr=ilist[1]+ilist[2]repeatDic[repeatStr]=repeatDic.get(xing,0)+1passelif namelen==4:if ilist[1]==ilist[2]passif ilist[2]==ilist[3]pass'''#统计姓的函数def Xing():xinglist=Open()[0]myDict={}#姓的字典for Xing in xinglist:myDict[Xing]=myDict.get(Xing,0)+1tmplist=list(myDict.items())tmplist.sort(key=lambda x:x[1],reverse=True)#print(tmpList[:200]),输出前200姓for i in tmplist:print(i[0],i[1],sep=',')#统计常用字的函数def ChangYongZi():mylist=[]minglist=Open()[1]for ilist in minglist:for everyChr in ilist:mylist.append(everyChr)myDic={}for i in mylist:myDic[i]=myDic.get(i,0)+1tmplist=list(myDic.items())tmplist.sort(key=lambda x:x[1],reverse=True)for i in tmplist[:200]:print(i[0],i[1],sep='/t')#统计重名的函数def ChongMing():minglist=Open()[1]afterMinglist=list(filter(lambda x:len(x)>=2,minglist))#名中有两个字重复 。说明名字长队得大于等于3chongMinglist=[]for i in afterMinglist:if len(i)==2 and i[0]==i[1]:chongMinglist.append(i)#i[1:]输出姓后面的名,刨除姓 。elif len(i)==3 and (i[0]==i[1] or i[1]==i[2]):chongMinglist.append(i[1:])#print(chongMinglist) 。重名的各个字在列表中 。输出['宸', '宸'], ['东', '东']tmplist=[]for ilist in chongMinglist:ming=''.join(ilist)tmplist.append(ming)#print(tmplist),输出'瑶瑶', '盈盈myDict={}for ming in tmplist:myDict[ming]=myDict.get(ming,0)+1newlist=list(myDict.items())newlist.sort(key=lambda x:x[1],reverse=True)for i in newlist[:200]:print(i[0],i[1],sep='\t')#输出前200对myfile=open('1700014174_work01.txt','w',encoding='utf-8')ChangYongZi()Xing()ChongMing()myfile.close()
简单数据的数学应用 中位数
下面这段代码是错误的:
myList=[12,34,45]def Zhong():N=len(myList)mySum=sum(myList)if N%2==0:tmp=myList[N//2-1]+myList[N//2]zhong=tmp//2else:zhong=myList[(N+1)//2-1]return zhongprint(Zhong())