python123 模拟试卷D综合编程题


1.AC代码展示


import wordcloud
import jieba
from imageio import imread


def zishu(fi1,fo1):
    ls1=[]
    d1={}
    for line in fi1:
        for c in line:
            if c!='\n':
                d1[c]=d1.get(c,0)+1
    ld1=sorted(d1.items(),\
              key=lambda x:x[1],reverse=True)
    #返回元组列表
    count=0
    for dui in ld1:
        count+=1
        ls1.append("{}:{}".format(dui[0],dui[1]))
        if count==100:
            break
    fo1.write(",".join(ls1))
    prols=[]
    for dui in ld1:
        prols.append("{}".format(dui[0]))
    protxt="".join(prols)
    return protxt

def Samezifu(txt1,txt2,fp):
    ls=[]
    for c in txt1:
        if c in txt2:
            ls.append(c)
    fp.write(",".join(ls))    
    
def clo1(fi1,fi2,fo1,fo2,fp):
    fi1.close();fi2.close();fo1.close()
    fo2.close();fp.close()
    
def clo2(fi1,fi2):
    fi1.close();fi2.close()

def ciyun(fi1,mingzi):
    mask=imread("tian.jpg")
    t=fi1.read()
    jb=jieba.lcut(t)
    txt=" ".join(jb)
    w=wordcloud.WordCloud(font_path=\
                          "C:\\Windows\\Fonts\\simkai.ttf",\
                          mask=mask,width=1000,height=700,\
                          background_color="white")
    w.generate(txt)
    w.to_file(mingzi)

def main():
    fi1=open("命运-网络版.txt","r",encoding="utf-8")
    fi2=open("寻梦-网络版.txt","r",encoding="utf-8")
    fo1=open("命运-字符统计.txt","w",encoding="utf-8")
    fo2=open("寻梦-字符统计.txt","w",encoding="utf-8")
    fp=open("相同字符.txt","w",encoding="utf-8")
    txt1=zishu(fi1,fo1)
    txt2=zishu(fi2,fo2)
    Samezifu(txt1,txt2,fp)
    clo1(fi1,fi2,fo1,fo2,fp)

    #文件句柄用一次就要重新来
    fi1=open("命运-网络版.txt","r",encoding="utf-8")
    fi2=open("寻梦-网络版.txt","r",encoding="utf-8")

    ciyun(fi1,"命运ciyun1.bmp")
    ciyun(fi2,"寻梦ciyun2.bmp")
    clo2(fi1,fi2)

main()


2.需要注意的点


1.imread不再在scipy里了
2. jieba.lcut生成的是字符串列表
3. 词云的生成用的是空格分隔的长字符串
4. html5语言和markdown语言之间要加空格,比如图片前,html5语言行间加上br标签

3.生成的词云展示


python123 模拟试卷D综合编程题_第1张图片

tian.jpg(mask文件)

python123 模拟试卷D综合编程题_第2张图片

命运ciyun1.bmp

python123 模拟试卷D综合编程题_第3张图片

寻梦ciyun2.bmp

你可能感兴趣的:(python二级)