删除列表中除gmails之外的所有电子邮件

时间:2017-07-11 19:54:17

标签: python python-3.x

我正在尝试过滤emailList以仅打印gmails。该列表包含以逗号分隔的多个电子邮件,供每个人使用。我想在保持相应名称的顺序的同时检索每个人的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)

1 个答案:

答案 0 :(得分:5)

支票应为.endswith('@gmail.com')。但是,电子邮件地址不区分大小写(赞誉为@kwhicks):

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

但你最好使用 list comprehension (替换整个 for循环):

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