Reputation: 73
I need to create a function which returns email addresses from a list of strings. I have done this successfully with the following code:
def myfunction(bigstring):
str = ' '.join(bigstring)
my_string = str
str1 = re.findall(r'[\w\.-]+@[\w\.-]+', my_string)
return str1
However when I run the following sample:
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
myfunction(emails)
I get the output:
['[email protected]', '[email protected]', '[email protected]', '[email protected]']
However i would like the following output, but I am not sure how to do so without messing up my code:
[['[email protected]', [email protected]'], ['[email protected]'], ['[email protected]']]
I need to return a list within a list as shown by the desired output.
Upvotes: 0
Views: 2217
Reputation: 27
def myfunction(bstr):
#str = ' '.join(bstr)
#my_string = str
str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bstr)
return str1
import re
output=[]
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam
Hartley <[email protected]>','Ben Saunders <[email protected]>']
for item in emails:
output.append(myfunction(item))
Upvotes: 0
Reputation: 2159
def myfunction(bigstring):
#str = ' '.join(bigstring)
#my_string = str
str1 = re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)
return str1
import re
output=[]
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
for item in emails:
output.append(myfunction(item))
print(output)
Upvotes: 1
Reputation: 3964
Dont join the whole string in function and pass the string one by one by iteration like this
import re
def myfunction(bigstring):
return re.findall(r'[\w\.-]+@[\w\.-]+', bigstring)
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
output = []
for emailstring in emails:
output.append((myfunction(emailstring)))
print(output)
using list comprehension
output = [ myfunction(email) for email in emails ]
print(output)
using map
print(map(myfunction,emails))
Output
[['[email protected]', '[email protected]'], ['[email protected]'], ['[email protected]']]
Upvotes: 1
Reputation: 1837
You don't need to join the initial array of email containing strings.
def my_function(str_array):
return [re.findall(r'[\w\.-]+@[\w\.-]+', s) for s in str_array]
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
my_function(emails)
Upvotes: 2
Reputation: 627082
Extract emails in each list item:
import re
emails = ['John Kennedy <[email protected]> or <[email protected]>','Adam Hartley <[email protected]>','Ben Saunders <[email protected]>']
def myfunction(bigstring):
result = []
for s in bigstring:
result.append(re.findall(r'[\w.-]+@[\w.-]+', s))
return result
print(myfunction(emails))
# => [['[email protected]', '[email protected]'], ['[email protected]'], ['[email protected]']]
See the Python demo.
Actually, bigstring
is a bad name here as it is a list. Consider renaming to, say, my_list
or something.
As for the regex, you do not need to escape dots inside character classes.
Upvotes: 1