10000条代码Planning⑬

一、string库的调用

##调用string库
import string

#26个英文字母小大写
print(string.ascii_letters)

#26个英文字母大写
print(string.ascii_uppercase)

#26个英文字母小写
print(string.ascii_lowercase)

#10个阿拉伯数字
print(string.digits)

#特殊符号
print(string.punctuation)

二、random库的调用

##调用random库
#random.sample(random.sample(sequence, k)
#从序列 sequence 中随机选择 k 个不重复的元素。
import random
lst = [1, 2, 3, 4, 5]
sample = random.sample(lst, 3)  # 从 lst 中随机选择 3 个不重复的元素
print(sample)

#random.choice(sequence)
#从序列 sequence 中随机选择一个元素
import random
lst = [1, 2, 3, 4, 5]
choice = random.choice(lst)  # 从 lst 中随机选择一个元素
print(choice)

#random.choices(sequence[, weights=None]:可选参数, k=1)
#从序列 sequence 中根据权重随机选择 k 个元素,并允许重复选择
import random
lst = [1, 2, 3, 4, 5]
weights = [0.1, 0.2, 0.3, 0.2, 0.2]  # 每个元素的权重
choices = random.choices(lst, weights=weights, k=3)  # 从 lst 中根据权重随机选择 3 个元素
print(choices)

三、re正则库的调用

##调用正则re库
# 步骤一般为
# 1.import re              1.导入re库
# 2._regex=re.compile()    2.创建正则对象
# 3.mo=_regex.search(内容)  3.输入被查找的内容,返回'第一次'匹配的对象
# 4.mo.group()             4.返回匹配,(\d)(\d)代表两个z分组

#步骤也可简化
# 1.import re                       1.导入re库
# 2.mo=re.search('正则规则',内容)     2.输入被查找的内容,返回'第一次'匹配的对象
# 3.mo.group()  

"""
re正则常用:
\d  0-9任何数字
\D  除0-9任何数字以外的任何字符
\w  任何字母、数字或下划线字符
\W  除字母、数字或下划线字符以外的任何字符
\s  空格、制表符或换行符
\S  除空格、制表符或换行符以外的任何字符
.   除换行符以外的任何字符
.*  匹配所有字符
记忆wds,没想起来的话就想想wps
"""

##e.g.:提取身份证的出生年月份
import re
_regex=re.compile(r'\d{7}(\d{4})(\d{2})(\d{2})\d{4}')   #'{}'代表特定次数
mo=_regex.search(f'My Id is 4407652189012250069.')
#简化可写
#mo=re.search('\d{7}(\d{4})(\d{2})(\d{2})\d{4}',f'My Id is 4407652189012250069.')

#身份证号提取
print(mo.group())   #输出:'4407652189012250069'
print(mo.group(0))  #输出:'4407652189012250069'

#提取出生年份
print(mo.group(1))  #输出:'1890'

#提取出生月份
print(mo.group(2))  #输出:'12'

#提取出生日份
print(mo.group(3))  #输出:'25'

#提取出生日期
print(f'出生时间为 : {mo.group(1)}-{mo.group(2)}-{mo.group(3)}')     #输出:'出生时间为 : 1890-12-25'

#注意groups()
print(mo.groups())  #输出:'('1890', '12', '25')'

##re的管道匹配:'|'
import re
mo=re.search('Sheldon(Lin|Xia|Li)','SheldonLi is a handsome guy')
print(mo.group(1))      #输出:'Li'
print(mo.group())       #输出:'SheldonLi'

##re的可选匹配:'?'
import re
mo=re.search('Sheldon(Lin)?','Sheldon is a handsome guy')
print(mo.group(1))      #输出:'None'
print(mo.group())       #输出:'Sheldon'

##re的匹配0次或多次:'*'
import re
mo=re.search('Sheldon(Lin)*','Sheldon is a handsome guy')
print(mo.group())       #输出:'Sheldon'
mo=re.search('Sheldon(Li)*','SheldonLi is a handsome guy')
print(mo.group())       #输出:'SheldonLi'

##re的匹配1次或多次:'+'
import re
mo=re.search('Sheldon(Lin)+','Sheldon is a handsome guy')
#print(mo.group())       #输出:'AttributeError: 'NoneType' object has no attribute 'group''至少要有得匹配
mo=re.search('Sheldon(Li)+','SheldonLi is a handsome guy')
print(mo.group())       #输出:'SheldonLi'
mo=re.search('Sheldon(Li)+','SheldonLiLi is a handsome guy')
print(mo.group())       #输出:'SheldonLiLi'

##re的贪心:'{n,k}'、非贪心匹配:'{n,k}?'
import re
#贪心匹配
mo=re.search('Sheldon(Li){1,2}','SheldonLiLi is a handsome guy')
print(f'贪心匹配:{mo.group()}')         #输出:'贪心匹配:SheldonLiLi'
#非贪心匹配
mo=re.search('Sheldon(Li){1,2}?','SheldonLiLi is a handsome guy')
print(f'非贪心匹配:{mo.group()}')        #输出:'非贪心匹配:SheldonLi'

##re的findall方法,返回类型字符串元素的列表或元组元素的列表
import re
#正则规则没有分组,返回字符串元素的列表
print(re.findall('Sheldon\D{1,4}','SheldonLiLi is a handsome guy,so do SheldonLi.'))    #输出:'['SheldonLiLi', 'SheldonLi']'
#正则规则1分组,返回字符串元素的列表
print(re.findall('Sheldon(\D{1,4})','SheldonLiLi is a handsome guy,so do SheldonLi.'))    #输出:'['LiLi', 'Li.']'
#正则规则2或以上分组,返回元组元素的列表
print(re.findall('(Sheldon)(\D{1,4})','SheldonLiLi is a handsome guy,so do SheldonLi.'))    #输出:'[('Sheldon', 'LiLi'), ('Sheldon', 'Li.')]'

#re的建立自己的字符分类
"""
[aeiouAEIOU]    匹配所有元音字符
[a-z]           匹配26个小写字母
[A-Z]           匹配26个大写
[0-9]           匹配0-9所有数字
插入'^'表示非,例如
[^0-9]          匹配非0-9的任何字符
"""
import re
text='hello123456world'
mo=re.findall('[0-9]',text)
print(mo)       #输出:'['1', '2', '3', '4', '5', '6']'
mo=re.findall('[^0-9]',text)
print(mo)       #输出:'['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']'

#re的匹配开头与结束
#开头:'^'
import re
text=['8hello','world3','25']
mo=[string for string in text if re.search('^[0-9]', string)]
print(mo)       #输出:'['8hello', '25']'
#结束:'$'
mo=[string for string in text if re.search('[0-9]$', string)]
print(mo)       #输出:'['world3', '25']'
#开头+结束
mo=[string for string in text if re.search('^[0-9]+$', string)]
print(mo)       #输出:'['25']'

#re的'.*'点星贪心与非贪心匹配
#贪心匹配
import re
text='My first name is <Sheldon>,last name is <Li>'
mo=re.search('My first name is <(.*)>',text)
print(mo.group())       #输出:'My first name is <Sheldon>,last name is <Li>'
#非贪心匹配
mo=re.search('My first name is <(.*?)>',text)
print(mo.group())       #输出:'My first name is <Sheldon>'

#re的compile参数:'re.DOTALL'
#匹配第一个换行符之前的字符
import re
text='hello\nworld'
_regex=re.compile('.*')
mo=_regex.search(text)
print(mo)       #输出:'<re.Match object; span=(0, 5), match='hello'>'
#匹配所有换行符
_regex=re.compile('.*',re.DOTALL)
mo=_regex.search(text)
print(mo)       #输出:'<re.Match object; span=(0, 11), match='hello\nworld'>'

#re的compile参数:'re.I'同're.IGNORECASE'
#不区分大小写匹配
import re
_regex=re.compile('[a-z]+',re.I)
mo=_regex.findall('Hello')
print(mo)       #输出:'['Hello']'

#re的compile参数:'re.VERBOSE',添加注释:'''
import re
_regex=re.compile('''[a-z]+  #所有小写字母''',re.VERBOSE)
mo=_regex.findall('Hello')
print(f'含注释:{mo}')      #输出:'含注释:['ello']'

#re的compile所有参数写法
#_regex=re.compile('''[a-z]+  #所有小写字母''',re.VERBOSE|re.I|re.DOTALL)

#re的sub()替换字符
text='5859Hello World!'
_regex=re.compile('[0-9]+')
mo=_regex.sub("",text)
print(mo)   #输出:'Hello World!'

如果你真的想当程序员那么就先写一万条代码。……《向上生长》
10号写下113条代码

目标:10000条

剩余:9280条

总结:

新学习了正则库,学习速度很慢,但是越来越上手。期待自己写够一万条代码的时候。

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值