如何从python列表中提取特定电子邮件?

时间:2018-10-19 02:08:33

标签: python list

我有大量电子邮件,并且我尝试仅提取好的电子邮件。问题在于域很多,因为它们除了标准的gmail域外还可能具有一些自定义域。我正在尝试从列表中删除公司域名。这是我的代码示例。当我运行以下代码时,我将获得列表中的所有电子邮件。

data = ['test@statefarm.com','test@gmail.com', 'test@yahoo.com', 'test@edwardjones.com']

#I want to remove the domains with statefarm.com or edwardjones.com
for email in data:
    if "statefarm.com" not in email or "edwardjones.com" not in email:
    # I have even tried but it still hasn't worked. 
    #if "statefarm.com" or "edwardjones.com" not in email:
        print(email)

1 个答案:

答案 0 :(得分:1)

正如@djukha所说,将or替换为and,这样做:

data = ['test@statefarm.com','test@gmail.com', 'test@yahoo.com', 'test@edwardjones.com']
for email in data:
    if "statefarm.com" not in email and "edwardjones.com" not in email:
        print(email)

但更好的是:

data = ['test@statefarm.com','test@gmail.com', 'test@yahoo.com', 'test@edwardjones.com']
print('\n'.join(filter(lambda x: any(i in x for i in {"statefarm.com","edwardjones.com"}),data)))