查看列表中的项是否在字符串Python 2.7中

时间:2016-04-06 09:47:48

标签: python

我正在尝试查看列表(域)中的项目是否在字符串(电子邮件)中,如果是,则打印该电子邮件中包含域名。 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!")

3 个答案:

答案 0 :(得分:2)

您必须分别对列表中的每个项目进行包含检查。虽然有一些语法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

答案 1 :(得分:0)

您的逻辑在您自己的代码中是错误的,但最简单和最快捷的方法是切片.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("."):]

如果你想继续询问,你应该使用while循环:

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

然后sinply:

if email.endswith(domains)

答案 2 :(得分:0)

您只是想查看电子邮件地址是否包含域,然后您可以简单地使用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