繁体   English   中英

寻找清单中的项目是否在字串Python 2.7中

[英]Looking to see if an item in a list is in a string Python 2.7

我试图查看列表(域)中的项目是否在字符串(电子邮件)中,如果是,则打印电子邮件中是否包含域。 Python告诉我“'in'需要字符串作为左操作数,而不是列表”,并且不会让我将它们进行比较。 有谁知道如何将列表中的单个项目与字符串进行比较? 提前致谢!

at = '@'
period = '.'
domain = ['.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult', ]
email = raw_input("What is your email? > ")


if period not in email:
    print("You failed")
else:
    print("You have an @ symbol!")


if period not in email:
    print("You do not have a period in your email")
else:
    print("You have a period in your email")


if domain not in email:
    print("You do not have a domain in your e-mail. Oops!")
else:
    print("You have a domain in your email!")

您必须分别对列表中的每个项目进行包含检查。 尽管有一些语法上的Python糖:

if any(x in email for x in domain):
    # good: email contains one of the domains

或更多(验证电子邮件):

if all(not email.endswith(x) for x in domain):
    # rain hellfire on user

您的逻辑在您自己的代码中是错误的,但是最简单,最快的方法是对.whatever进行切片并在一组域中进行查找:

domains = {'.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult',}


email = raw_input("What is your email? > ")

dom = email[email.rfind("."):]

如果您想继续询问,应该使用一会儿True循环:

at = '@'
period = '.'
domains = {'.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult',}
while True:
    email = input("What is your email? > ")
    if at not in email:
        print("You failed, no @ in your email addrress.")
        continue
    print("You have an @ symbol!")

    if period not in email:
        print("You do not have a period in your email")
        continue
    print("You have a period in your email")

    if email[email.rfind("."):]  in email:
          print("You have a domain in your email!")
          break          
    print("You do not have a domain in your e-mail. Oops!")

print(email)

如果要使用endswith,则只需传递一个域的元组即可:

domains = ('.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult')

然后sin:

if email.endswith(domains)

您只想查看电子邮件地址是否包含域,则可以简单地在python中使用find函数,该函数将返回获取子字符串的索引。

这是我的方式

domain = ['.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult', ]


email = raw_input("What is your email? > ")

for dom in domain:

     if(email.find(dom) != -1):
             print email

暂无
暂无

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

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