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")