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. css 浮动布局,清除浮动

    浮动的特性: (1)浮动元素有左浮动(float:left)和右浮动(float:right)两种 (2)浮动的元素会向左或向右浮动,碰到父元素边界.其他元素才停下来 (3)相邻浮动的块元素可以并在一 ...

  2. css 制作菜单

    代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  3. Java项目中启动Tomcat报错invalid LOC header

    原因: 可能是jar包有问题. 解决方法: 1.找到加载不了的类对应的jar包. 2.在tomcat中webapps/INF/lib中找到对应的jar包,然后删除. 3.重新下载其它版本的jar包. ...

  4. 剑指offer——python【第38题】二叉树的深度

    题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 解题思路 想了很久..首先本渣渣就不太理解递归在python中的实现 ...

  5. TSL 访问器

    设计原理:GE有一个分布式内存基础设施,成为内存云.内存云由一组内存主干组成.集群中的每台机器承载256个内存中继.我们将一台机器的本地内存空间划分为多个内存中继的原因有两方面:1)中继级别的并行性可 ...

  6. flink with rabbitmq,sink source mysql redis es

    flink-dockerhttps://github.com/melentye/flink-docker https://shekharsingh.com/blog/2016/11/12/apache ...

  7. [strongswan][autoconf][automake][cento] 在CentOS上编译strongswan git源码时遇到的autoconf问题

    编译strongswan的git源码问题 1. 概述 首先,我们想要通过源码编译strongswan.当满足以下条件时,通常你会遇见此问题: 源码时通过git clone的得来的,而不是官网下载的源码 ...

  8. JAVA RPC (六) 之thrift反序列化RPC消息体

    我们来看一下服务端的简单实现,直接上thrift代码,很直观的来看一看thrift的server到底干了些什么 public boolean process(TProtocol in, TProtoc ...

  9. k8s-N0.4-service

    本章目录 k8s中的三种网络 service的构建及参数说明 一  k8s的三种网络 在k8s集群中,k8s是有三种网络类型的,下面我们看一下下面这个图 1 节点网络:顾名思义,节点网络就是你每台物理 ...

  10. oracle多行合并一行

    以上图为例 执行SQL语句: select d.group_id,to_char(wm_concat(d.tag)) from Imglib_Group_Tag d where d.group_id= ...