python - 删除列表中的所有电子邮件,不包括 gmails

标签 python python-3.x

我正在尝试过滤 emailList 以仅打印 gmail。该列表包含每个人的多封电子邮件,以逗号分隔。我想在保持相应名称顺序的同时检索每个人的 gmail。

class engineerInfo:
firstName = ""
lastName = ""
email = ""
title = ""

engineers = []

for col in rows:
    e = engineerInfo()
    e.firstName = col[0]
    e.lastName = col[1]
    e.email = col[2]
    e.title = col[3]
    engineers.append(e)

while True:
    print("1- Print gmails of software engineers")
    choice = int(input("Choose from the menu:"))

if choice == 1:
    emailList = []
    for i in engineers:
        if i.email not in emailList:
            emailList.append(i.email)

    gmailList = []
    for i in emailList:
        if i != 'gmail.com':
            continue
        else:
            gmailList.append(i)    
    print(gmailList)

最佳答案

检查应该是 .endswith('@gmail.com')。但是,电子邮件地址不区分大小写,例如(@kwhicks 的荣誉):

gmailList = []
for i in emailList:
    if <b>i.lower().endswith('@gmail.com')</b>:
        gmailList.append(i)
print(gmailList)

但是您最好为此使用列表理解(替换整个 for 循环):

gmailList = [i for i in emailList if i.lower().endswith('@gmail.com')]

关于python - 删除列表中的所有电子邮件,不包括 gmails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043448/

相关文章:

python 根据出现次数替换单词

python - 在python中将字典列表拆分为多个字典列表

java - 在 (debian 9) python :3 docker image using terminal commands 上安装 java 8

python - 给定 1/4/2014 时如何在 python 3.0 中从 mm/dd/yyyy 格式转换为 yyyy-mm-dd

python - 确定 TD 标签内的类

python - Python 的 xml.etree.ElementTree 叶元素的意外 bool 行为

python - 从 pandas 数据框(库存)计算数据的更有效方法

python - 在 Django 中从命令行创建用户

python - NLTK Sentence Tokenizer,自定义句子启动器

python - readthedocs突然无法安装scipy