繁体   English   中英

从 Python 中的字符串列表返回 email 地址的 function

[英]A function that returns email addresses from a list of strings in Python

我需要创建一个 function ,它从字符串列表中返回 email 地址。 我已使用以下代码成功完成此操作:

def myfunction(bigstring):
    str = ' '.join(bigstring)
    my_string = str
    str1 = re.findall(r'[\w\.-]+@[\w\.-]+', my_string)
    return str1

但是,当我运行以下示例时:

emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']
myfunction(emails)

我得到 output:

['jk123@gmail.com', 'johnk123@hotmail.com', 'ah123@yahoo.com', 'benji@live.co.uk']

但是我想要以下 output,但我不知道如何在不弄乱我的代码的情况下这样做:

[['jk123@gmail.com', johnk123@hotmail.com'], ['ah123@yahoo.com'], ['benji@live.co.uk']]

我需要在列表中返回一个列表,如所需的 output 所示。

您不需要加入包含字符串的 email 的初始数组。

def my_function(str_array):
    return [re.findall(r'[\w\.-]+@[\w\.-]+', s) for s in str_array]

emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']

my_function(emails)

提取每个列表项中的电子邮件:

import re

emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']
def myfunction(bigstring):
    result = []
    for s in bigstring:
        result.append(re.findall(r'[\w.-]+@[\w.-]+', s))
    return result

print(myfunction(emails))
# => [['jk123@gmail.com', 'johnk123@hotmail.com'], ['ah123@yahoo.com'], ['benji@live.co.uk']]

请参阅Python 演示

实际上, bigstring在这里是个坏名字,因为它是一个列表。 考虑重命名为my_list或其他名称。

至于正则表达式,您不需要在字符类中转义点。

不要在 function 中加入整个字符串,并像这样通过迭代一个一个地传递字符串

import re
def myfunction(bigstring):
    return  re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)

emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']


output = []
for emailstring in emails:
    output.append((myfunction(emailstring)))
print(output)

使用列表理解

output = [ myfunction(email) for email in emails ]
print(output)

使用map

print(map(myfunction,emails))

Output

[['jk123@gmail.com', 'johnk123@hotmail.com'], ['ah123@yahoo.com'], ['benji@live.co.uk']]
def myfunction(bigstring):
    #str = ' '.join(bigstring)
    #my_string = str
    str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)
    return str1

import re
output=[]
emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']
for item in emails:
    output.append(myfunction(item))

print(output)
    def myfunction(bstr):
        #str = ' '.join(bstr)
        #my_string = str
        str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bstr)
        return str1
    import re
    output=[]
    emails = ['John Kennedy <jk123@gmail.com> or <johnk123@hotmail.com>','Adam 
              Hartley <ah123@yahoo.com>','Ben Saunders <benji@live.co.uk>']
     for item in emails:
        output.append(myfunction(item))

暂无
暂无

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

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