【问题标题】:Python replace in listPython 在列表中替换
【发布时间】:2018-11-08 07:55:02
【问题描述】:

我有一个包含不同电子邮件格式的列表,我想将其统一为一个。 有两种类型的电子邮件:

name.surname@dom
name.surname.extern@dom

我的程序允许用户输入电子邮件,并且不必一直输入"@dom"(总是相同的),我所做的是允许用户写name.surnamename.surname.e然后脚本将这些用户名替换为 @dom.extern@dom 当我将所有不同格式的邮件存储在一个列表中时,问题就出现了,我希望它们符合标准,这样如果我有

["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]

一切都变成了这样

["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]

我尝试了列表推导,但我得到的只是三个串联:

["%s.xtern@dom" % x for x in mails if x[-2:] == ".e"] +
["%s@dom" %x for x in mails if "@dom not in mails" and x[-2:] != ".e"] + 
[x for x in mails if "@dom" in x]

我确信有一种更好的方法,不需要我做 3 次列表推导,也不需要我做

for i,v in enumerate(mails):
    if "@dom" not in v:
        mails[i] = "%s@dom" % v
    etc.

【问题讨论】:

    标签: python list list-comprehension


    【解决方案1】:

    您可以使用字符串的endswith() 方法来确定您需要对输入做什么:

    mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
    
    final_mails = []
    
    for mail in mails:
        if mail.endswith("@dom"):
            # Use as-is if it ends with @dom.
            final_mails.append(mail)
        elif mail.endswith(".e"):
            # Replace to extern@dom if it ends with .e
            final_mails.append(mail.replace(".e", ".extern@dom"))
        else:
            # Add @dom on all other cases
            final_mails.append("{}@dom".format(mail))
    
    print final_mails
    
    # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
    

    可能需要更彻底的检查才能不接受名称中间的@dom 之类的东西。希望对您有所帮助!

    编辑: 只是为了好玩,如果您坚持列表理解:

    mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
    
    final_mails = ["{}@dom".format((mail.replace(".e", ".extern@dom") 
                   if mail.endswith(".e") else mail).rstrip("@dom")) 
                   for mail in mails]
    
    print final_mails
    
    # Result: ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
    

    就我个人而言,我发现列表推导在简短易读时是最好的,所以我会坚持第一个选项。

    【讨论】:

      【解决方案2】:

      没有列表理解的选项:

      maillist = ["john.doe@dom", "john2.doe",    "john3.doe.e","john4.doe.extern@dom"]
      
      for i in range(len(maillist)): 
          if "@" not in maillist[i]:
              if maillist[i][-2:]== ".e": 
                  maillist[i] += ("xtern@dom") 
              else: 
                  maillist[i] +=  "@dom"
      

      【讨论】:

        【解决方案3】:

        @Green Cell 比我快,他的回答似乎是正确的。这是一个做同样事情的列表推导:

        mails = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
        
        print mails
        
        mails = [mail if mail.endswith("@dom") else mail.replace(".e", ".extern@dom") if mail.endswith(".e") else "{}@dom".format(mail) for mail in mails]
        
        print mails
        

        哪些输出:

        ['john.doe@dom', 'john2.doe', 'john3.doe.e', 'john4.doe.extern@dom']
        ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
        

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:

          如果你想要单行,请将list comprehensionmultiple if/else statements 结合起来:

          first_list = ["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
          
          second_list = [email + 'xtern@dom' if email.endswith(".e") else \
                         email if email.endswith("@dom") else "{}@dom".format(email) \
                             for email in first_list]
          
          print second_list
          

          给予:

          ['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
          

          【讨论】:

            【解决方案5】:

            最后我提出了一个不同的解决方案:声明一个帮助函数。

            def mailfy(mail):
               if mail.endswith(".c"):
                 return mail[:-2] + "@dom"
               ...
            
            mails = [mailfy(x) for x in mails]
            

            【讨论】:

              猜你喜欢
              • 2011-09-21
              • 1970-01-01
              • 2018-01-11
              • 1970-01-01
              • 2021-06-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-26
              相关资源
              最近更新 更多