Python数据类型结构( 五 )


注意:中位数,要先把所给各个数字按照大小顺序排列 。这是中位数的定义 。所以修改代码如下:
'''myList=[12,34,45,34,34,4]def fZhongWei(myList):myList.sort()N=len(myList)if N%2==1:zhong=myList((N+1)//2)else:zhong=(myList(N//2)+myList(N//2-1))//2#应该是方括号return zhongzhong=fZhongWei(myList)print(zhong)#列表 。'''myList=[1,2,4,5]def fZhongWei(myList):myList.sort()N=len(myList)if N%2==1:zhong=myList[(N+1)//2-1]else:zhong=(myList[N//2-1]+myList[N//2])//2return zhongzhong=fZhongWei(myList)print(zhong)
质因数 质数
求质数或者质因数,代码如下:
def isPrime(N):if N<2:return Falseif N==2:return Truefor i in range(2,N):if N%i==0:return Falseelse:return TrueprimeList=[i for i in range(1,1000) if isPrime(i)==True]def myIsPrime(N):i=2while i1:#while条件可以尝试以最后结果为准 。求质数,从2,3……一直到N,i1if N%i==0:N=N//imyList.append(i)print(i)input()else:i+=1return myListprint(ZhiYinShu(1468))
寻找质因数,需要用到循环语句 。
def ZhiYinShu(N):i=2myList=[]while N>1:#while条件可以尝试以最后结果为准 。求质数,从2,3……一直到N,i1if N%i==0:N=N//imyList.append(i)print(i)input()else:i+=1return myListN=int(input(":"))for i in range(2,N):#for循环中i每次自动加了1,while循环中i得手动加1while N>1:if N%i==0:print(i)N=N//icontinueelse:breakelse:break
随机数
随机数,需要用大函数 。
import randomprint(random.choice(range(1,1000)))#输出1000以内的随机数
复杂数据 图片文本
文本数据本质就是字符串 。对于字符串的操作,可以应用到文本上面 。
打开docx文本
import docxfile=docx.Document("test.docx")wfile=open("result.txt","w",encoding="utf-8")for p in file.paragraphs:line=p.textif "=" in line:line=line.strip("=")print(line,file=wfile)else:myline=line.split(" ")for word in myline[3:]:print("\t",word,sep=" ",end="",file=wfile)print("\n",file=wfile)wfile.close()
对文本切分
按照行,按照逗号切分
rFile=open("HZ.txt","r",encoding="utf-8")rData=http://www.kingceram.com/post/rFile.read()rFile.close()rData=rData.split("\n")for line in rData[:10]:tmp=line.split(",")print(tmp[0],"对应的数据是",tmp[1],tmp[2])
文言文词频统计
def InterTxt():import jieba#中文文章需要分词才能进行词频统 。一定一定主义,我把安装包移动到了同一个文件夹后,才能使用import调用jiebafile1=open("file1.txt","rt",encoding="GB18030")file2=open("file2.txt","rt",encoding="GB18030")data1=file1.read()#这个read必不可少,可以连在一起写f=open("Hong.txt","rt",encoding="utf-8").read()data2=file2.read()file1.close()file2.close()for ch in '!,.:;[\\]@&*:“”‘’;/~`?,●。· ampqot?、\u3000 \\u()!\n<>hr【】&,l,;,《》1,2,3,4,5,,6,5,8.9,0.,7,':data1 = data1.replace(ch, "")data2 = data2.replace(ch, "")words1=jieba.lcut(data1)words2=jieba.lcut(data2)#print(words1)return words1,words2InterTxt()def WordCOuntDic():words=InterTxt()word1=words[0]word2=words[1]counts1={}counts1Singel={}for word in word1:if len(word) == 1:if word in counts1Singel:counts1Singel[word]+=1else:counts1Singel[word]=1else:if word in counts1:counts1[word] += 1else:counts1[word] = 1counts2={}counts2Singe2={}#单字词单独列一个表格for word in word2:if len(word)==1:if word in counts2Singe2:counts2Singe2[word]+=1else:counts2Singe2[word]=1else:if word in counts2:counts2[word] += 1else:counts2[word] = 1#excludes={"什么","一个","我们","那里",}#删去不需要的词#for word in excludes:# 注意,excludes 字典中的词语得存在,如果不存在,遍历循环的时候就会出错 。#del(counts1[word])#del(counts2[word])#del(counts2Singe2[word])#del(counts1Singel[word])items1=list(counts1.items())#把字典列表化,便于使用items2=list(counts2.items())#把字典列表化,便于使用items1Singel=list(counts1Singel.items())items2singel=list(counts2Singe2.items())items1.sort(key=lambda x:x[1],reverse=True)#排序,按照数量items2.sort(key=lambda x:x[1],reverse=True)# 排序,按照数量items1Singel.sort(key=lambda x:x[1],reverse=True)items2singel.sort(key=lambda x:x[1],reverse=True)#print(items1)return items1,items2,items1Singel,items2singeldef OutResult():items=WordCOuntDic()itemsDouble1=items[0]itemsDouble2=items[1]itemsSingle1=items[2]itemsSingle2=items[3]wFile= open("GuDaiCiyu.txt", "wt", encoding="GB18030")# r,raw的格式 。print("文件一:",file=wFile)for everyWord in itemsDouble1[:100]:print(everyWord[0], everyWord[1], file=wFile)for everyWord in itemsSingle1[:100]:print(everyWord[0], everyWord[1], file=wFile)print("文件二:",file=wFile)for everyWord in itemsDouble2[:100]:print(everyWord[0], everyWord[1], file=wFile)for everyWord in itemsSingle2[:100]:print(everyWord[0], everyWord[1], file=wFile)wFile.close()OutResult()