字符串a = ‘not 404 found 张三 99 深圳’,每个词中间是空格,用正则过滤掉英文和数字,最终输出’张三 深圳’
import re
a ="not 404 found 张三 99 深圳"
res = re.findall("\d+|[a-zA-Z]+", a)
a_l = a.split(" ")for i in res:ifstr(i)in a_l:
a_l.remove(i)print(" ".join(a_l))# 张三 深圳
x = 'abc'
y = 'def'
z = ['d', 'e', 'f']
print(x.join(y)) # dabceabcf
print(x.join(z)) # dabceabcf
python中交换两个数值
a =1
b =2
a, b = b, a
print(a, b)# 2 1
举例说明zip()函数用法
"""
zip()函数在运算时,会以一个或多个序列(可迭代对象)做为参数,返回一个元组的列表。同时将这些序列中并排的元素配对。
zip()参数可以接受任何类型的序列,同时也可以有两个以上的参数;当传入参数的长度不同时,zip能自动以最短序列长度为准进行截取,获得元组
"""# 列表
a =[1,2]
b =[3,4]
res =[i for i inzip(a, b)]print(res)# [(1, 3), (2, 4)]# 元组
a =(1,2)
b =(3,4)
res =[i for i inzip(a, b)]print(res)# [(1, 3), (2, 4)]# 字符串
a ="ab"
b ="xyz"
res =[i for i inzip(a, b)]print(res)# [('a', 'x'), ('b', 'y')]
a=‘张明 98分’,用re.sub,将98替换为100
import re
a="张明 100分"
ret=re.sub(r"\d+","100",a)print(ret)# 张明 100分
a='hello’和b='你好’编码成bytes类型
a ="hello"print(bytes(a.encode("utf-8")))# b'hello'
b ="你好"print(bytes(b.encode("utf-8")))# b'\xe4\xbd\xa0\xe5\xa5\xbd'
[1,2,3]+[4,5,6]的结果是多少?
[1,2,3,4,5,6]
正则匹配,匹配日期2018-03-20
import re
url='https://sycm.taobao.com/bda/tradinganaly/overview/get_summary.json?dateRange=2018-03-20%7C2018-03-20&dateType=recent1&device=1&token=ff25b109b&_=1521595613462'
result=re.findall(r'dateRange=(.*?)&',url)print(result)# ['2018-03-20%7C2018-03-20']
list=[2,3,5,4,9,6],从小到大排序,不许用sort,输出[2,3,4,5,6,9]
li =[2,3,5,4,9,6]
new_li =[]defget_min(li):
a =min(li)
li.remove(a)
new_li.append(a)iflen(li)>0:
get_min(li)return new_li
get_min(li)print(new_li)# [2,3,4,5,6,9]
import re
s ="info:xiaoZhang 33 shandong"# |表示或,根据冒号或者空格切分
res = re.split(r":| ", s)print(res)# ['info', 'xiaoZhang', '33', 'shandong']
正则匹配以163.com结尾的邮箱
import re
email_list =["xiaoWang@163.com","xiaoWang@163.comheihei",".com.xiaowang@qq.com"]for email in email_list:
ret = re.match("[\w]{4,20}@163\.com$", email)if ret:print("%s 是符合规定的邮件地址,匹配后结果是:%s"%(email, ret.group()))# xiaoWang@163.comelse:print("%s 不符合要求"% email)# xiaoWang@163.comheihei .com.xiaowang@qq.com
递归求和
defget_sum(num):if num >=1:
res = num + get_sum(num -1)else:
res =0return res
print(get_sum(10))# 55
python字典和json字符串相互转化方法
import json
dic ={"name":"zs"}
res = json.dumps(dic)print(res,type(res))# {"name": "zs"} <class 'str'>
ret = json.loads(res)print(ret,type(ret))# {'name': 'zs'} <class 'dict'>
统计字符串中某字符出现次数
str="张三 哈哈 张三 呵呵 张三"
res =str.count("张三")print(res)# 3
字符串转化大小写
s ="HHaa"print(s.upper())# HHAAprint(s.lower())# hhaa
用两种方法去空格
str="hello world ha ha"
res =str.replace(" ","")print(res)# helloworldhahalist=str.split(" ")
res ="".join(list)print(res)# helloworldhaha
正则匹配不是以4和7结尾的手机号
tels =["13100001234","18912344321","10086","18800007777"]for tel in tels:
ret = re.match("1\d{9}[0-3,5-6,8-9]", tel)if ret:print("结果是:", ret.group())# 13100001234 18800007777else:print("%s不是想要的手机号"% tel)# 18912344321 10086
正则匹配中文
import re
title ="你好,hello,世界"
res = re.compile(r'[\u4e00-\u9fa5]+')
result = res.findall(title)print(result)# ['你好', '世界']
正则表达式匹配出
itcast.cn
import re
labels=["<html><h1>python编程:887934385</h1></html>","<html><h1>python编程:887934385</h2></html>"]for label in labels:
ret=re.match(r"<(\w*)><(\w*)>.*?</\2></\1>",label)if ret:print("%s是符合要求的标签"%ret.group())# <html><h1>python编程:887934385</h1></html>else:print("%s不符合要求"%label)# <html><h1>python编程:887934385</h2></html>
int(‘1.4’),int(1.4)输出结果?
print(int("1.4"))# 报错ValueErrorprint(int(1.4))# 1
求两个列表的交集、差集、并集
a =[1,2,3,4]
b =[4,3,5,6]
jj1 =[i for i in a if i in b]# 在a中的i,并且也在b中,就是交集
jj2 =list(set(a).intersection(set(b)))
bj1 =list(set(a).union(set(b)))
cj1 =list(set(b).difference(set(a)))
cj2 =list(set(a).difference(set(b)))print("交集", jj1)# 交集 [3, 4]print("交集", jj2)# 交集 [3, 4]print("并集", bj1)# 并集 [1, 2, 3, 4, 5, 6]print("差集", cj1)# 差集 [5, 6]print("差集", cj2)# 差集 [1, 2]