python - 关于列表中正则表达式使用的一个简单问题
问题描述
我在下面的列表中有一个列表为“list_all”,我正在寻找下面“c”中所述的单词。第二个列表中没有“c”。下面的代码给出的结果为 ['c', 'c'] 但我希望 ['c', '', 'c'] 具有相同的长度 'list_all'。请你帮我解决一下,我怎样才能把空元素放到结果中。
import re
list_all = [['a','b','c','d'],['a','b','d'],['a','b','c','d','e']]
listofresult =[]
for h in [*range(len(list_all))]:
for item in list_all[h]:
patern = r"(c)"
if re.search(patern, item):
listofresult.append(item)
else:
None
print(listofresult)
解决方案
尝试这个
import re
list_all = [['a','b','c','d'],['a','b','d'],['a','b','c','d','e']]
temp = True
listofresult =[]
for h in range(len(list_all)):
for item in list_all[h]:
patern = r"(c)"
if re.search(patern, item):
listofresult.append(item)
temp = False
if temp:
listofresult.append("")
temp = True
print(listofresult)
推荐阅读
- powershell - 在 Word 中执行 powershell 查找替换时如何使变量大小写敏感
- apache - RewriteRule [F,L] 在 Apache 日志中显示 500 而不是 403
- r - R中glm插入符号模型的方差膨胀VIF
- html - xslt中列表元素内的分隔斜体和跨度标签
- javascript - 在自动完成中禁用退格删除选项
- javascript - AJAX 在滚动到底部时加载更多
- linux - 用于 CentOS / Red Hat 7.8 的 Pericom 串行端口驱动程序(正确的 baud_base)
- sql - 我们有针对 T-SQL CONCAT_WS 函数的解决方法吗?
- python - 如何拆分pyspark数据框并创建新列
- linux - 在星期二的每个月的奇数天运行 cron 作业?