已知字符串 a_str = '404 not found 张三 23 深圳', 每个词中间都是空格, 要求只输出字符串中的中文?
方法一:
使用正则表达式: \w+, re.A即指ASCII编码, 可匹配除中文以外的单词字符, 得到新列表
利用 去同存异 的方法
a_str = '404 not found 张三 23 深圳'
import re
a_list = a_str.split(" ") # ['404', 'not', 'found', '张三', '23', '深圳']
res = re.findall(r'\w+', a_str, re.A) # ['404', 'not', 'found', '23']
new_list = []
for i in a_list:
if i not in res:
new_list.append(i)
print(" ".join(new_list))
# 输出结果
张三 深圳