在英语中英文单词组成句子,在键盘上输入一句含有标点英文语句,统计其中最长的单词以及各单词出现的次数。具体要求如下:
(1)用户输入一个英文语句,英文语句子中要包含标点符号;若个别单词后面带有标点符号,其标点符号不能计算为单词;
(2)输出英文语句中最长的单词,若有多个单词,则每行输出一个;
(3)统计各单词出现的次数,按照数量从多到少排列在屏幕上输出;如果出现的数量相同,则按照单词在句子中的顺序进行显示;输出的格式为:单词左对齐,宽度为10。
例如,如果用户在键盘上输入句子:Hi, we play on the Maldive beach on October the second. 则输出如下图所示:
填空:
import string
sent = input().strip()
for c in string.punctuation:
sent = _________________(c," ") #1去除标点符号
wordlist = sent.split() #分词
wordcounts = {}
wordlens =[]
for word in wordlist:
wordcounts[word] = wordcounts.get(_______) + 1 #2词频统计
for word in wordcounts.keys():
wordlens.append((word,len(word))) #计算每个单词长度
sort_wordcounts = sorted(________,key = lambda x:x[1],reverse =True) #3按照词频排序
sort_wordlens = sorted(wordlens,key = lambda x:x[1],____________) #4按照单词长度排序
maxlen = _________ #5最长单词的长度
for ________ in sort_wordlens: #6输出所有最长单词
if wlen == maxlen:
print("maxlen:{1:<3}word:{0:>10}".format(word,wlen))
else:
__________ #7
for word, count in sort_wordcounts: #输出单词词频
print("{0:<10}{1:->5}".format(word,count)) #8
补全后的代码如下:
import string
sent = input().strip()
for c in string.punctuation:
sent = sent.replace(c, " ") # 1去除标点符号
wordlist = sent.split() # 分词
wordcounts = {}
wordlens = []
for word in wordlist:
wordcounts[word] = wordcounts.get(word,0) + 1 # 2词频统计
for word in wordcounts.keys():
wordlens.append((word, len(word))) # 计算每个单词长度
sort_wordcounts = sorted(wordcounts.items(), key=lambda x: x[1], reverse=True) # 3按照词频排序
sort_wordlens = sorted(wordlens, key=lambda x: x[1], reverse=True) # 4按照单词长度排序
maxlen = sort_wordlens[0][1] # 5最长单词的长度
for word, wlen in sort_wordlens: # 6输出所有最长单词
if wlen == maxlen:
print("maxlen:{1:<3}word:{0:>10}".format(word, wlen))
else:
pass
for word, count in sort_wordcounts: # 输出单词词频
print("{0:<10}{1:->5}".format(word, count)) # 8
运行结果:
import sys
import pygame
pygame.init()
#icon = pygame.image.load("biu.jpg")
#pygame.display.set_icon(icon) #设置窗口图标
#Vinfo = pygame.display.Info() #获取当前操作系统的屏幕尺寸
#size = width, height = Vinfo.current_w, Vinfo.current_h #将系统的屏幕尺寸作为显示窗口的尺寸
size = width,height = 900 ,600 #设置屏幕的长度和宽度
speed = [1,1] #设置移动速度[x,y]
BLACK = 0,0,0 #设置背景颜色
#screen = pygame.display.set_mode(size) #初始化显示窗口
screen = pygame.display.set_mode(size,pygame.RESIZABLE) #将屏幕设置为大小可调
#screen = pygame.display.set_mode(size,pygame.NOFRAME) #将屏幕设置为无边框
#screen = pygame.display.set_mode(size,pygame.FULLSCREEN) #将屏幕设置为全屏模式
pygame.display.set_caption("壁球游戏") #将窗口命名为“壁球游戏”
ball = pygame.image.load("biu.jpg") #加载图片
ballrect = ball.get_rect() #返回一个覆盖图像的矩形Rect对象,载入图像的外切矩形
"""Rect对象有一些重要的属性,比如top、bottom、left、right表示上下左右
width、height表示宽度和高度"""
fps = 300
fclock = pygame.time.Clock()
still = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
#按左键改变壁球左右移动方向,按一次方向取反
if event.key == pygame.K_LEFT:
speed[0] = -speed[0]
#按上键改变壁球上下移动方向,按一次方向取反
elif event.key == pygame.K_UP:
speed[1] = -speed[1]
elif event.key == pygame.K_ESCAPE:
sys.exit("游戏结束!")
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
"""当鼠标左键按下时,将控制壁球移动的变量设置为True,
让壁球停止移动,因为后边是用if pygame.display.get_active() and not still:
来控制壁球移动的,将still取反了"""
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
"""当鼠标按键释放时,让壁运动的相对球移动到鼠标所在的位置,同时让壁球继续运动
move函数的参数是指两次距离,将两个位置的相对距离赋值给函数
"""
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1: # 当鼠标左键按下的时候
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
"""当鼠标左键处于按下状态并且在移动时,使壁球的图片跟随着鼠标一起移动"""
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0],speed[1]) #在横轴和纵轴移动x,y像素
""" 设置窗口感知,当显示为界面的时候,返回True,图标化的时候返回False
这样设置如果将窗口最小化,壁球会停止运动
当打开的时候壁球将会继续运动"""
#判断图像的边缘位置,如果碰到上下左右边缘,使其速度取反
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
#sys.exit("Game Over!")
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = - speed[0]
"""如果壁球的右边缘的坐标位置大于屏幕宽度,则让壁球改变运动方向"""
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
#sys.exit("Game Over")
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = - speed[1]
screen.fill(BLACK) #填充背景颜色
screen.blit(ball,ballrect)
"""将一个图像绘制在另一个图像上,即将ball绘制到ballrect位置上。
通过Rect对象引导对移动图像的绘制,让图像跟着外切矩形的移动而移动
这样就形成了图像的移动"""
pygame.display.update() #刷新屏幕,仅仅重新绘制窗口中有变化的地方,比flip函数速度更快,flip函数重新绘制整个窗口
fclock.tick(fps)