首页 > 解决方案 > Using a conditional comprehesion and the re.search function to find all the strings in a list?

问题描述

I'm trying to use a conditional comprehesion and the re.search function to find all the strings in DNA_list that start with 'ATG' and end with 'TAG'

DNA_list = ['GTCTCTCGA', 'ATGCCTGAAGCATTCTAG', 'GCTGCCCACAAG', 'ATGACTGTAAAACCCTAG']
import re
dna=[print(element)for element in DNA_list if re.search(r'(^ATG)(TAG$)',str(DNA_list))]

But I don't get an output. What am I missing?

标签: pythonlistconditionallist-comprehension

解决方案


result = [s for s in DNA_list if re.match(r'^ATG.*TAG$', s)]

推荐阅读