1、英文单词出现频率排行统计程序:
import re
with open('老人与海片段.txt','r') as f:
article = f.read()
new_article = re.sub('^[a-zA-Z\s]','',article)
new_article = new_article.split() #
word_counts = {}
for word in new_article:
if word.upper() in word_counts:
word_counts[word.upper()] = word_counts[word.upper()] +1
else:
#
#这是是相当于字典里面的get()方法
word_counts[word.upper()] = 1
# key_list = list(word_counts.items())
data = sorted(word_counts.items(),key = lambda x:x[1],reverse=True)[0:5]
for item in data:
print("{:4s}-{:4d}".format(item[0],item[1]))
2、中文词出现频率统计程序:(好像中文单个词出现没有意义)
import re
with open('鲁迅.txt','r') as f:
article = f.read()
new_article = re.sub('[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]','',article)
new_article = str(new_article).replace('\r','')
new_article = str(new_article).replace('\n','')
new_article = new_article.strip()
new_article = re.findall('[\u2E80-\u9FFF]',new_article)
print(new_article)
# for item in new_article:
# new_article.remove("u\u3000")
# print(new_article)
# 这里不能用到for里面的这个迭代器,因为for里面的这个数据结构像极了C语言里面的指针,
一旦删除了一个元素,后面的另一个元素就会被推前来,代替这个元素,所以这个元素就会被遗忘,
不再被迭代出来。所以就会删除的有剩余,不能用迭代器里面删除。
for i in range(len(new_article)-1,-1,-1):
if new_article[i] == '\u3000':
new_article.pop(i)
word_counts = {}
for word in new_article:
if word.upper() in word_counts:
word_counts[word.upper()] = word_counts[word.upper()] +1
else:
#这是是相当于字典里面的get()方法
word_counts[word.upper()] = 1
# key_list = list(word_counts.items())
data = sorted(word_counts.items(),key = lambda x:x[1],reverse=True)[0:10]
print(data)
for item in data:
print("{:4s}-{:4d}".format(item[0],item[1]))
3、 程序流程控制的应用
1. 可以让用户输入学生的座位号以及姓名
2. 提供菜单功能
3. 可以分别输入“语文”,“英语”,“数学”3科成绩
显示成绩单时,除了3科成绩之外,也要计算总分以及平均分。
import os
class_303 = dict() #记录学生座号及姓名
chinese_score = dict() #记录语文成绩
math_score = dict() #记录数学成绩
english_score = dict() #记录英语成绩
subjects = ['语文','数学','英语']
scores = [chinese_score,math_score,english_score]
# 主页面展示
def display_menu():
# 这里的clc用于windows系统,clear用于Linux
os.system('clear')
print("Class 303 班级成绩管理系统")
print("---------------------------------------")
print("1. 输入学生姓名")
print("2. 输入语文成绩")
print("3. 输入数学成绩")
print("4. 输入英语成绩")
print("5. 显示成绩单")
print("0. 结束循环")
print("---------------------------------------")
# 输入座位姓名
def enter_seat_name():
while True:
# 这里的输入可以做一个异常输入
while True:
try :
seat = int(input("座位号(0==》停止输入:"))
break
except:
print("请输入数字(0-100):")
if seat <= 0 or seat >= 100:
break
name = input("姓名:")
class_303[seat] = name
# print(class_303)
print("{}:{}".format(seat,name))
# 输入成绩
def enter_score(subject_key):
for seat,name in class_303.items():
while True:
try:
scores[subject_key][seat] = int(input("座位号为{},{}的{}成绩:".format(seat,name,subjects[subject_key])))
break
except:
print("请输入正确的分数(数字)")
print(scores[subject_key])
x = print("按Enter返回主菜单")
# 显示成绩单
def display_score_table():
for seat in class_303.keys():
print("{:<5}:".format(class_303[seat],end=''))
sum = 0
for subject_key in range(0,3):
sum = sum + scores[subject_key][seat]
print("{}:{:>}".format(subjects[subject_key],scores[subject_key][seat],end=''))
print("总分:{:3>},平均:{:.2f}".format(sum,float(sum)/len(scores)))
x = input("按Enter返回主菜单")
if __name__ == '__main__':
while True:
display_menu()
user_choice = int(input("请输入你的选择:"))
if user_choice == 1:
enter_seat_name()
elif user_choice>=2 and user_choice<=4:
enter_score(user_choice - 2)
elif user_choice == 5:
display_score_table()
else:
break
4.请设计一个程序,可以按用户的输入模拟摇骰子的次数。然后列出每个数字出现的百分比
import random
def test():
while True:
try:
num = int(input("请输入掷骰子的次数:"))
break
except:
print("请输入数字:")
return num
if __name__ == '__main__':
# 记录每次掷骰子的点数
dice = dict()
# 记录每个骰子出现的概率
probability = dict()
num = test()
if num <=0:
print("请输入正整数")
test()
for time in range(1,num+1):
dice[time] = random.randint(1,6) #记录每次的骰子的点数
if dice[time] in probability.keys():
probability[dice[time]] = probability[dice[time]] +1
else:
probability[dice[time]] = 1
#这种方法显示出来不太美观,
# for pro in probability.keys():
# print("{:<2}:{:>4.2%}".format(pro,float(probability[pro])/num))
probability = list(probability.items())
data = sorted(probability,key=lambda x:x[0],reverse=False)
# for pro in data:
# print("{:<2}:{:>4.2%}".format(pro[0],float(pro[1])/num),end="\000")
# print("")
#事实证明,\000 空 和 \b 退格都没有用
for pro in data:
print("{:<2}:{:>4.2%}".format(pro[0],float(pro[1])/num),end="\v")
# print()
# for pro in data:
# print("{:<2}:{:>4.2%}".format(pro[0],float(pro[1])/num),end="\b")