1.字符串操作:& 2.英文词频统计预处理

1.字符串操作:

解析身份证号:生日、性别、出生地等。

ID = input('请输入十八位身份证号码: ')
if len(ID) == 18:
print("你的身份证号码是 " + ID)
else:
print("错误的身份证号码") ID_add = ID[0:6]
ID_birth = ID[6:14]
ID_sex = ID[14:17]
ID_check = ID[17] # ID_add是身份证中的区域代码,如果有一个行政区划代码字典,就可以用获取大致地址# year = ID_birth[0:4]
moon = ID_birth[4:6]
day = ID_birth[6:8]
print("生日: " + year + '年' + moon + '月' + day + '日') if int(ID_sex) % 2 == 0:
print('性别:女')
else:
print('性别:男') # 此部分应为错误判断,如果错误就不应有上面的输出,如何实现?#
W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
ID_num = [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
ID_CHECK = ['', '', 'X', '', '', '', '', '', '', '', '']
ID_aXw = 0
for i in range(len(W)):
ID_aXw = ID_aXw + int(ID[i]) * W[i] ID_Check = ID_aXw % 11
if ID_check == ID_CHECK[ID_Check]:
print('正确的身份证号码')
else:
print('错误的身份证号码')

运行结果如下

1.字符串操作:& 2.英文词频统计预处理

凯撒密码编码与解码

MAX_KEY_SIZE = 26
def getMode():
while True:
print('请选择加密或解密模式,或者选择暴力破解:')
print('加密:encrypt(e)')
print('解密:decrypt(d)')
print('暴力破解:brute(b)')
mode = input().lower()
if mode in 'encrypt e decrypt d brute b'.split():
return mode
else:
print('请输入"encrypt"或"e"或"decrypt"或"d"或"brute"或"b"!')
def getMessage():
print('请输入你的信息:')
return input()
def getKey():
key = 0
while True:
print('请输入密钥数字(1-%s)' % (MAX_KEY_SIZE))
key = int(input())
if (key >=1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode, message, key):
if mode[0] == 'd':
key = -key
translated = ''
for symbol in message:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += symbol
return translated
mode = getMode()
message = getMessage()
if mode[0] != 'b':
key = getKey()
print('你要翻译的信息是:')
if mode[0] != 'b':
print(getTranslatedMessage(mode, message, key))
else:
for key in range(1, MAX_KEY_SIZE + 1):
print(key, getTranslatedMessage('decrypt', message, key))

1.字符串操作:& 2.英文词频统计预处理

网址观察与批量生成

print(r"搜索结果如下");
url="https://list.jd.com/list.html?tid=1006238"
s="&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s="
print("第1页网址为{}".format(url));
for i in range(5):
arg=url+s+str(i*44);
print("第{}页网址为{}".format(i+2,url)); ##运行结果如下

1.字符串操作:& 2.英文词频统计预处理

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说,保存为utf8文件。
  • 从文件读出字符串。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词

并统计单词出现的次数。

#英文歌词:
str1='''I will not make the same mistakes that you did
I will not let myself cause my heart so much misery
I will not break the way you did
You fell so hard
I learned the hard way, to never let it get that far
-
Because of you
I never stray too far from the sidewalk
Because of you
I learned to play on the safe side
So I don't get hurt
Because of you
I find it hard to trust
Not only me, but everyone around me
Because of you
I am afraid
-
I lose my way
And it's not too long before you point it out
I cannot cry
Because I know that's weakness in your eyes
I'm forced to fake a smile, a laugh
Every day of my life
My heart can't possibly break
When it wasn't even whole to start with
-
Because of you
I never stray too far from the sidewalk
Because of you
I learned to play on the safe side
So I don't get hurt
Because of you
I find it hard to trust
Not only me, but everyone around me
Because of you
I am afraid
-
I watched you die
I heard you cry
Every night in your sleep
I was so young
You should have known better than to lean on me
You never thought of anyone else
You just saw your pain
And now I cry
In the middle of the night
Over the same damn thing
-
Because of you
I never stray too far from the sidewalk
Because of you
I learned to play on the safe side so I don't get hurt
Because of you
I tried my hardest just to forget everything
Because of you
I don't know how to let anyone else in
Because of you
I'm ashamed of my life because it's empty
Because of you
I am afraid
-
Because of you'''
#把单词全部变成小写
s1=str1.lower()
print(s1)
#去掉空格
str1=str1.lstrip()
print(str1)
#将歌词的每个单词分隔组成列表形式
print("将歌词的每个单词分隔组成列表形式:")
strList=str1.split()
print(strList)
#计算单词出现的次数
print("计算单词出现的次数:")
strSet=set(strList)
for word in strSet:
print(word,strList.count(word))

运行结果如下

1.字符串操作:& 2.英文词频统计预处理

上一篇:[SimplePlayer] 4. 从视频文件中提取音频


下一篇:Jenkins使用QQ邮箱构建邮件提醒服务