简体   繁体   English

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

[英]How do I extract specific emails from list in python?

I have a huge list of emails and I have tried to extract only the good emails. 我有大量的电子邮件,并且我尝试仅提取好的电子邮件。 The problem is that the domains are many since they might have some custom domain, in addition to the standard gmail domains. 问题在于域很多,因为它们除了标准的gmail域外还可能具有一些自定义域。 I am trying to take out the corporate domains from the list. 我正在尝试从列表中删除公司域名。 Here is an example of my code. 这是我的代码示例。 When I run the below code, I get all the emails in the list. 当我运行以下代码时,我将获得列表中的所有电子邮件。

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)

As @djukha says, replace or to and , so do: 作为@djukha说,更换orand ,这样做:

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)

But for even better: 但更好的是:

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM