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('错误的身份证号码')

运行结果如下

凯撒密码编码与解码

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))

网址观察与批量生成

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)); ##运行结果如下

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.英文词频统计预处理的更多相关文章

  1. Python——字符串、文件操作,英文词频统计预处理

    一.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 2.凯撒密码编码与解码 凯撒加密法的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左 ...

  2. python字符串操作、文件操作,英文词频统计预处理

    1.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 解析身份证号:生日.性别.出生地等 def function3(): print('请输入身份证号') ...

  3. Programming | 中/ 英文词频统计(MATLAB实现)

    一.英文词频统计 英文词频统计很简单,只需借助split断句,再统计即可. 完整MATLAB代码: function wordcount %思路:中文词频统计涉及到对"词语"的判断 ...

  4. 组合数据类型,英文词频统计 python

    练习: 总结列表,元组,字典,集合的联系与区别.列表,元组,字典,集合的遍历. 区别: 一.列表:列表给大家的印象是索引,有了索引就是有序,想要存储有序的项目,用列表是再好不过的选择了.在python ...

  5. Hadoop的改进实验(中文分词词频统计及英文词频统计)(4/4)

    声明: 1)本文由我bitpeach原创撰写,转载时请注明出处,侵权必究. 2)本小实验工作环境为Windows系统下的百度云(联网),和Ubuntu系统的hadoop1-2-1(自己提前配好).如不 ...

  6. python复合数据类型以及英文词频统计

    这个作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753. 1.列表,元组,字典,集合分别如何增删改查及遍历. 列 ...

  7. 英文词频统计的java实现方法

    需求概要 1.读取文件,文件内包可含英文字符,及常见标点,空格级换行符. 2.统计英文单词在本文件的出现次数 3.将统计结果排序 4.显示排序结果 分析 1.读取文件可使用BufferedReader ...

  8. python:Hamlet英文词频统计

    #CalHamletV1.py def getText(): #定义函数读取文件 txt = open("hamlet.txt","r").read() txt ...

  9. python学习day3------列表、元组、字符串操作

    一.列表 变量名后加中括号[],接下来介绍对列表进行查操作 #!/usr/bin/env python #-*- Coding:utf-8 -*- # Author:Eric.Shen test = ...

随机推荐

  1. [bzoj1051]Popular Cows

    刚刚被ysy在联考里虐了,差点爆tan(pi/4),只好来bzoj寻求安慰再被虐一次233 (tarjan是什么智障东西不想打我好弱啊,tarjan都不会打) Description 每一头牛的愿望就 ...

  2. Docker 安装以及运用

    Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上.Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位.系统内核版 ...

  3. Django中web开发用md5加密图片名并存储静态文件夹

    一般在开发中,有的网站存在大量图片,首先图片的名称是不能重复的, 但是除了数据库可用的id以外我们可以用time模块中time.time()获取的时间来进行md5加密操作, 因为time模块所产生的时 ...

  4. 阿里云入坑指南&&nodejs 安装配置

    买了阿里云1G1核1M的机器(800元,3年) 登录阿里云-实例-选择实例所在地区-重置密码 用公网IP ssh连接 #升级CentOS yum -y update #安装or更新组件 yum -y ...

  5. docker上配置mysql主从复制

    1.在docker上启动2台mysql容器:(这里3306为主,3307为从) docker run -d  -e MYSQL_ROOT_PASSWORD=123456  -p 3306:3306 - ...

  6. 读书笔记一【哈希——MD5】

    计算机中,将某种数据转换成指定范围内的数字数字或字母叫做哈希(散列.hashing) 哈希后的值无法像加密解密那样恢复为原文值,通常用于文件校验或数字签名等 好的散列算法应具有: 充分利用所有数据位 ...

  7. 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)

    背景 博主的主要工作是测试API,目前已经用Jmeter+Jenkins实现了项目中的接口自动化测试流程.但是马上要接手的项目,API应用的是OAuth2.0协议授权,并且采用的是简化模式(impli ...

  8. 浏览器User-Agent大全

    what's the User-Agent UserAgent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,UserAgent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你 ...

  9. vue 封装组件

    props 接收数据 props对象里面 键值 是对改数据的 数据类型 的规定.做了规范,使用者就只能传输指定类型的数据,否则报警告 先根据要求写出完整的代码,再一一用参数实现组件封装 这里试着封装一 ...

  10. try catch的使用场景