失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python零基础入门习题(六)红楼梦人物出场次数统计

Python零基础入门习题(六)红楼梦人物出场次数统计

时间:2021-08-22 20:08:09

相关推荐

Python零基础入门习题(六)红楼梦人物出场次数统计

前言

Python语言简单易用,可读性强。在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。

一、生日悖论

生日悖论是指在不少于 23 个人中至少有两人生日相同的概率大于 50%。

请你验证一下。

import randomn=int(input("样本次数:"))cou=0for num in range(n):birth=[]for i in range(60):a=random.randint(1,365)birth.append(a)if len(birth)!=len(set(birth)):cou+=1print("{:.2f}%".format(cou/n*100))

二、绘制函数

import turtleimport mathturtle.speed(0)turtle.penup()turtle.goto(200,200)turtle.pendown()turtle.write("n=1 黑色 \n n=3 绿色 \n n=5 红色 \n n=7 黄色",False,'center',font=('arial',10,'bold')) #提示词for n in range(1,8,2):turtle.penup()turtle.goto(-600,10*4*math.sin(0)/math.pi)turtle.pendown()if n==3: turtle.color('green') #改变函数颜色elif n==5: turtle.color('red')elif n==7:turtle.color('yellow')x=0while(x<=50):y=0for k in range(1,n+1,2): #绘制函数y=y+4*math.sin(k*x)/(k*math.pi)turtle.goto(20*x-600,20*y)x+=0.02turtle.exitonclick()

三、随机密码生成

请编写程序随机生成八组包含大小写字母及数字的密码。

import randomlist=[]password=()for i in range(65,91):list.append(chr(i))for i in range(97,123):list.append(chr(i))for i in range(10):list.append(i) #大小写字母及数字for i in range(10):password=tuple(random.sample(list,8)) #随机密码for j in range(8):print(password[j],end="")print("\n")

四、找出红楼梦中出场次数前30位的人物

提示:利用jieba库

#-*- coding : utf-8-*-# coding:unicode_escapeimport jiebatxt = open(r'dizhi\红楼梦.txt', 'r',encoding='GB18030').read()# 利用分词函数lcut(),把txt中的句子分成词汇,存储在words中words = jieba.lcut(txt)#定义一个空集合或字典 countscounts = {}excludes={"一个","quot","什么","我们","那里","如今","你们","说道","知道","起来","这里","出来","姑娘","他们","众人","奶奶","自己","一面","只见","两个","怎么","不是","不知","这个","听见","这样","进来","咱们","告诉","就是","东西","回来","大家","没有","只是","只得","这些","丫头","不敢","出去","所以","不过","不好","姐姐","过来","的话","一时","不能","心里","如此","银子","今日","答应","几个","二人","还有","说话","只管","一回","这么","那边","外头","打发","自然","这话","那些","今儿","罢了","屋里","听说","小丫头","如何","问道","妹妹","看见","不用","人家","媳妇","原来","家里","一声","一句","不得","进去","到底","这会子","姊妹","回去","过去","别人","丫鬟","连忙","还是","婆子","心中","身上","里头","方才","只有","不成","哥哥","小厮","有人","于是","明白","这是","一日","起身","一件","那个","怎么样","已经","跟前","果然","有些","谁知","瞧瞧","不肯","只怕","不必","越发","难道","好些","主意","吩咐","况且","母亲","不如","喜欢","不要","一处","此时","便是","上来","因此","看着","不见","放心","女儿","素日","房中","想着","人来","还要","一样","在家","明日","言语","为什么","且说","不觉","别的","半日","多少","想起","然后","走来","正是","许多","正说","一般","跟着","一会"}#遍历变量words中的所有词汇#如果是一个字,则不进行统计(人名没有一个字的)#如果不是一个字,则counts[word](键值为word的词进行累计(counts[word] = counts.get(word,0) + 1))for word in words:if len(word) == 1: #排除单个字符的分词结果continueelif word == "老太太" or word == "太太" or word == "老祖宗" or word == "史太君":rword = "贾母"elif word == "王熙凤" or word == "熙凤" or word == "凤辣子" or word == "凤姐儿":rword = "凤姐"elif word == "林黛玉" or word == "黛玉道" or word == "林丫头" or word == "林妹妹":rword = "黛玉"elif word == "宝姑娘" or word == "宝丫头" or word == "蘅芜君" or word == "宝姐姐":rword = "宝钗"elif word == "老爷" or word == "贾政道":rword = "贾政"elif word == "宝二爷" or word == "贾宝玉":rword = "宝玉"else:rword=wordcounts[rword] = counts.get(rword,0) + 1#counts[word] = counts.get(word,0)+1 是对进行计数word出现的频率进行统计,当word不在words时,返回值是0,当word在words中时,返回+1,以此进行累计计数。for word in excludes:del(counts[word])items = list(counts.items())# 把counts的项(键值对)转成序列。items.sort(key=lambda x:x[1], reverse=True)#sort是对列表(items)进行排序的函数,reverse=False 是升序,默认,reverse=True是降序;<列表名>.sort(key=none, reverse=False)没有返回值,但对原序列进行排序。#那这个key=lambda x: x[1] 是什么意思呢,这个lambda是一个隐函数,在这里可以不用管它,记得有这个就可以,后面的x: x[1] 为对前面的对象中的第二维数据(即value)的值进行排序。#打印前30位信息键值对。for i in range(30):word, count = items[i]print ("{0:<10}{1:>5}".format(word, count))

五、打印5种食品

a,b="tomato","potato"food=(a,b,"cucumber","coconut","spinach")for j in range(5):print(food[j])

下面这种方法会报错,想一想,为什么?

a,b="tomato","potato"food=(a,b,"cucumber","coconut","spinach")food[1]="water"for j in range(5):print(food[j])

a,b="tomato","potato"food=(a,b,"cucumber","coconut","spinach")a,b="water","juice"food=(a,b,"cucumber","coconut","spinach")for j in range(5):print(food[j])

总结

以上就是本文全部内容,你学会了吗?

如果觉得《Python零基础入门习题(六)红楼梦人物出场次数统计》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。