• python初学(二)


    1.输入一个整数列表L,判断L中是否存在相同的数字:

    (1)若存在,输出YES,否则输出NO;

    (2)若存在,输出YES,同时输出相同的数字;否则输出NO。

    1 l=list(input())
    2 print(l)
    3 if len(l)==len(set(l)):
    4     print('NO')
    5 else:
    6     for i in list(set(l)):
    7         l.remove(i)
    8     print('YES',' '.join(l))

    2.从英文字母(区分大小写)和 0~9数字组成的列表中随机生成10个6位数的密码。

     1 import random
     2 
     3 l=[]
     4 l+=list(range(10))
     5 l+=[chr(i) for i in range(65, 91)]
     6 l+=[chr(i) for i in range(97, 123)]
     7 
     8 res=[]
     9 for i in range(10):
    10     s=''
    11     for j in range(6):
    12         s+=str(l[random.randint(0,len(l)-1)])
    13     res.append(s)
    14 print('
    '.join(res))

    3.从网上自行下载一个长篇英文小说,统计并输出该小说中词频最大的TOP 20结果。利用该文本和wordcloud库、imageio库等,生成一个属于自己的词云图形。

     1 import jieba
     2 import wordcloud
     3 import imageio
     4 
     5 txt=open("./trial-python/achristmascarol.txt","r",encoding="utf-8").read()
     6 words=jieba.lcut(txt)
     7 counts={}
     8 for word in words:
     9     if len(word)==1 and ord(word) not in range(65,91) and ord(word) not in range(97,123):
    10         continue
    11     else:
    12         counts[word]=counts.get(word,0)+1
    13 
    14 items=list(counts.items())
    15 items.sort(key=lambda x: x[1],reverse=True)
    16 for i in range(20):
    17     word,count=items[i]
    18     print("{0:<2}:{1:<10}{2:>5}".format(i+1,word,count))
    19 w=wordcloud.WordCloud(width=1000,height=700,background_color="white")
    20 w.generate_from_frequencies(counts)
    21 w.to_file("./trial-python/achristmascarol.png")

    4.从网上自行下载一个长篇中文小说(除课堂讲授的三国演义外),统计并输出该小说中词频最大的TOP 20结果。利用该文本和jieba库、wordcloud库、imageio库等,生成一个属于自己的词云图形。

    import jieba
    import wordcloud
    import imageio
    
    txt=open("./trial-python/thestoryofthestone.txt","r",encoding="utf-8").read()
    words=jieba.lcut(txt)
    counts={}
    for word in words:
        if len(word)==1:
            continue
        else:
            counts[word]=counts.get(word,0)+1
    items=list(counts.items())
    items.sort(key=lambda x: x[1],reverse=True)
    for i in range(20):
        word,count=items[i]
        print("{0:<2}:{1:<10}{2:>5}".format(i+1,word,count))
    w=wordcloud.WordCloud(width=1000,height=700,font_path="msyh.ttc",background_color="white")
    w.generate_from_frequencies(counts)
    w.to_file("./trial-python/thestoryofthestone.png")
  • 相关阅读:
    JUC锁框架_AbstractQueuedSynchronizer详细分析
    npm的镜像替换成淘宝
    MHA+keepalived集群环境搭建
    Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
    链表中倒数第k个结点
    调整数组顺序使奇数位于偶数前面
    数值的整数次方
    二进制中1的个数
    矩形覆盖
    OS之进程管理---多线程模型和线程库(POSIX PTread)
  • 原文地址:https://www.cnblogs.com/unknowcry/p/12730593.html
Copyright © 2020-2023  润新知