defmain(): x = int(input("请输入团队人数:")) nums = 0 for i inrange(100000): times = isRepeat(groupBirthdays(x)) if times == True: nums += 1 else: continue y = str(nums / 100000 * 100)[:5] print("假设一个团队有{}人,用随机模拟{}次,其中至少有两人生日相同的比例为{}%".format(x, 100000, y))
for i inrange(3): main()
文字出现频率
编写程序,对文档”讲话.txt”进行分析,按出现频率的降序打印字符(前20位)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import jieba
txt = open("F:\\习总书记在北京大学师生座谈会上的讲话 .txt","r",encoding='gbk',errors='ignore').read() lst = jieba.lcut(txt) cout = {} for ls in lst: ls = ls.replace(' ','') iflen(ls) == 1: continue else: cout[ls] = cout.get(ls,0)+1 items = list(cout.items()) items.sort(key=lambda x: x[1], reverse=True) for item inrange(20): word, count = items[item] print("词语:【{}】,出现次数:{}".format(word, count))