正则表达式迭代匹配列表中的元素

时间:2015-01-09 07:03:46

标签: python regex

我有一个如下所示的列表:

mylist = [
    'Th2 2w, total RNA (linc-sh36)',
    'SP CD8, total RNA (replicate 1)',
    'DN 2, total RNA (replicate 2)']

我想要做的是保持该列表中与其他列表匹配的条目:

ctlist = ['DN 1', 'DN 2', 'DN 3', 'DN 4', \
          'DP 1', 'DP 2', 'tTreg', 'CD8', 'CD4', 'iTreg']

所以最后的输出是产生这个:

 SP CD8, total RNA (replicate 1)
 DN 2, total RNA (replicate 2)

我尝试了这个,但没有产生结果:

import re
for mem in mylist:
    for ct in ctlist:
      regex = re.compile(ct)
      match = regex.match(mem)
      if match:
         print mem

做正确的方法是什么?

4 个答案:

答案 0 :(得分:2)

主要问题是您忘记了mylist中的逗号。所以你的数据不是你想象的那样。尝试添加一些打印语句,您可以在循环中轻松发现这样的问题。

第二个问题是您需要regex.search而不是regex.match,因为您尝试匹配整个字符串,而不仅仅是mem的开头。但是,对于您正在做的事情,您根本不需要正则表达式:

for mem in mylist:
    for ct in ctlist:
        if ct in mem:
            print mem
            break

答案 1 :(得分:1)

您的,值中缺少

mylist

mylist = [
    'Th2 2w, total RNA (linc-sh36)',
    'SP CD8, total RNA (replicate 1)',
    'DN 2, total RNA (replicate 2)']

我们可以在代码开头创建正则表达式模式,然后在for循环中使用。

代码:

mylist = [
    'Th2 2w, total RNA (linc-sh36)',
    'SP CD8, total RNA (replicate 1)',
    'DN 2, total RNA (replicate 2)']

ctlist = ['DN 1', 'DN 2', 'DN 3', 'DN 4', \
          'DP 1', 'DP 2', 'tTreg', 'CD8', 'CD4', 'iTreg']

import re
regex = re.compile("|".join(ctlist))
print [ mem for mem in mylist  if regex.match(mem)]

输出:

python test.py 
['DN 2, total RNA (replicate 2)']

答案 2 :(得分:1)

你在这里不需要正则表达式:

>>> mylist
['Th2 2w, total RNA (linc-sh36)', 'SP CD8, total RNA (replicate 1)', 'DN 2, total RNA (replicate 2)']
>>> ctlist
['DN 1', 'DN 2', 'DN 3', 'DN 4', 'DP 1', 'DP 2', 'tTreg', 'CD8', 'CD4', 'iTreg']
>>> [ x for x in mylist for y in ctlist if y in x]
['SP CD8, total RNA (replicate 1)', 'DN 2, total RNA (replicate 2)']

答案 3 :(得分:1)

mylist = ['Th2 2w, total RNA (linc-sh36)','SP CD8, total RNA (replicate 1)','DN 2, total RNA (replicate 2)']
ctlist = ['DN 1', 'DN 2', 'DN 3', 'DN 4','DP 1', 'DP 2', 'tTreg', 'CD8', 'CD4', 'iTreg']
print [ x for x in mylist if [y for y in ctlist if y in x ]]