Python列表理解嵌套循环
问题描述:
现在我通过映射一个新的列表来排除列表中的电子邮件,排除我不想要的东西。这看起来像:Python列表理解嵌套循环
pattern = re.compile('b\.com')
emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
emails = [e for e in emails if pattern.search(e) == None]
# resulting list: ['[email protected]', '[email protected]']
不过,现在我需要过滤掉多个域,所以我也需要被过滤掉域的列表。
pattern_list = ['b.com', 'c.com']
有没有办法做到这一点仍然在列表理解形式,或者我将不得不恢复到嵌套for循环?
注意:在@处分割字符串并且执行word[1] in pattern_list
将不起作用,因为c.com
也需要赶上sub.c.com
。
答
有几个方法可以做到这一点,即使不使用正则表达式。其一是:
[e for e in emails if not any(pat in e for pat in pattern_list)]
这也将排除像[email protected]
和[email protected]
电子邮件,但这样做你原来的解决方案。但是,它并不排除您现有解决方案所用的[email protected]
等案例。再一次,目前尚不清楚您现有的解决方案是否确实按您的想法做了。
另一种可能性是将你的模式与rx = '|'.join(pattern_list)
合并为一个,然后在该正则表达式上匹配。但是,如果您只想匹配b.com
作为完整域(而不仅仅是域的一部分或作为用户名的一部分),则需要使用更复杂的正则表达式。
答
import re
pattern = re.compile('b.com$|c.com$')
emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
emails = [e for e in emails if pattern.search(e) == None]
print emails
这个怎么
+0
好主意!..... – georg 2014-09-23 18:37:48
我不喜欢列表理解是解决这个问题的最好方法 - 你可能可以做到,但是很麻烦。看看这个解决方案:http://*.com/questions/19150208/python-search-regex-from-variable-inside-a-list – karthikr 2014-09-23 18:27:53
请注意,你现有的例子也将排除,例如'[email protected] '和'bob.com @ bob.com'。那是你要的吗? – BrenBarn 2014-09-23 18:28:28
当你对列表解析进行列表解析时,通常最好使用生成器(将方括号改为parens),它更高效地存储内存并很好地链接在一起。 – Seth 2014-09-23 18:30:53