04hanzi.py(汉字搜索)
# name: 04hanzi.py
# date: 20240609
# programmer: arithmus
# memo: find hanzi string
import re
def is_in(fullstr,substr):
if re.findall(substr,fullstr):
return 1
else:
return 0
a = '这个暑假我读了红楼梦和三国演义'
b= ['三国演义','水浒传','西游记','红楼梦']
line = []
n = 0
for i in b:
num = is_in(a,i)
n += num
res = re.findall(i,a)
line = line + res
print(f'四大名著暑假读了{n}本')
print(f'分别是{line}')
'''
res = re.findall(i,a) re.findall返回值是一个列表
out:
四大名著暑假读了2本
分别是['三国演义', '红楼梦']
'''