Python实验七

多是课件程序,反复练习即可,如有问题,可私信删除此文,谢谢

jieba库导入方法(在cmd命令中输入以下指令,如下载过慢,可采取换源 pip install 安装库 -i 源地址)本人用的是清华的:https://pypi.tuna.tsinghua.edu.cn/simple

pip install jieba 

1、编写程序打印99乘法表,要求使用format()进行格式化输出。

for i in range(1,10):
    for j in range(1,i+1):
        print('{:1}*{:0}={:02}'.format(j,i,i*j),end = ' ')
    print()

2、使用string模块提供的字符串常量,模拟生成指定长度的随机密码。

import random
from string import ascii_letters,digits
def generatePassword(n):
        res = ascii_letters + digits
        password = ''.join(random.sample(res,n))
        return password

count = int(input("请输入你要产生几位密码:"))
print(generatePassword(count))

3、通过精确模式、全模式和搜索引擎模式对文本“中华人民共和国是伟大的国家”进行分词,简述他们的异同。

import jieba
String = "中华人民共和国是伟大的国家"
print(jieba.lcut(String))
print(jieba.lcut(String,cut_all=True))
print(jieba.lcut_for_search(String))
#相同:都是找出字符串中可能组成词语的字符
#不同:精确模式:把文本精确的切分开,不存在冗余单词
#全模式:把字符串中所有可能的词语都找出来,有冗余
#搜索引擎模式: 在精确模式上对长词语进行细分

4、统计《三国演义》中出现次数较高的人物。(需选定老师指定文本,其中读文本操作,以后会学习到)

import jieba
excludes = {'先主','将军','却说','荆州','二人','不可',
            '不能','如此','忽然','下马','喊声','马岱',
            '商议','军士','左右','引兵','大喜','于是',
            '东吴','今日','如何','军马','天下','不敢',
            '魏兵','人马','不知','一人','众将','只见',
            '蜀兵','大叫','上马','此人','一面','太守',
            '后人','背后','何不','城中','忽报','先锋',
            '然后','大军','先生','何故','不如','令人',
            '赶来','江东','正是','徐州','因此','未知','大败',
            '百姓','一军','之后','接应','起兵','不见','进兵',
            '次日','陛下','汉中','天子','夫人','原来','成都',
            '大事','引军','可以','心中','大怒','军中'}
txt = open("E:\三国演义.txt", "r", encoding = 'utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word == '诸葛亮' or word == '孔明曰':
        rword = '孔明'
    elif word == '云长' or word == '关公':
        rword = '关羽'
    elif word == '燕人' or word == '翼德':
        rword = '张飞'
    elif word == '玄德曰' or word == '玄德':
        rword = '刘备'
    elif word == '孟德' or word == '丞相':
        rword = '曹操'
    elif word == '后主' or word == '阿斗':
        rword == '刘禅'
    elif word == '都督' or word == '公瑾':
        rword = '周瑜'
    elif word == '子敬' or word == '参军':
        rword = '鲁肃'
    elif word == '子龙':
        rword = '赵云'
    else:
        rword = word
    counts[rword] = counts.get(rword,0)+1
for word in excludes:
    del(counts[word])
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = True)
for i in range(15):
    word,count = items[i]
    print("{0:<10} {1:>5}".format(word,count))          

5、编程检查并判断密码字符串的安全强度。

import string
def check(pwd):
    if not isinstance(pwd,str) or len(pwd)<6:
        return "太简单了"
    strength = {1:'weak',2:'low middle',3:'above middle',4:'high'}
    list = [False]*4
    for ch in pwd:
        if not list[0] and ch in string.digits:
            list[0] = True
        elif not list[1] and ch in string.ascii_lowercase:
            list[1] = True
        elif not list[2] and ch in string.ascii_uppercase:
            list[2] =True
        elif not list[3] and ch in ',.!;?<>/;':
            list[3] = True
    return strength.get(list.count(True),'输入有误')
while True:
    password = input("请输入密码:")
    s = check(password)
    print(s)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值