Python replace in list
up vote
1
down vote
favorite
I have a list with different email formatting, which I want to unify all in one.
There are 2 types of email:
name.surname@dom
name.surname.extern@dom
My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have
["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
it all ends up looking like this
["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]
I have tried with list comprehensions, but all I got was three concatenations:
["%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]
I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do
for i,v in enumerate(mails):
if "@dom" not in v:
mails[i] = "%s@dom" % v
etc.
python list list-comprehension
add a comment |
up vote
1
down vote
favorite
I have a list with different email formatting, which I want to unify all in one.
There are 2 types of email:
name.surname@dom
name.surname.extern@dom
My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have
["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
it all ends up looking like this
["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]
I have tried with list comprehensions, but all I got was three concatenations:
["%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]
I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do
for i,v in enumerate(mails):
if "@dom" not in v:
mails[i] = "%s@dom" % v
etc.
python list list-comprehension
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a list with different email formatting, which I want to unify all in one.
There are 2 types of email:
name.surname@dom
name.surname.extern@dom
My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have
["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
it all ends up looking like this
["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]
I have tried with list comprehensions, but all I got was three concatenations:
["%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]
I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do
for i,v in enumerate(mails):
if "@dom" not in v:
mails[i] = "%s@dom" % v
etc.
python list list-comprehension
I have a list with different email formatting, which I want to unify all in one.
There are 2 types of email:
name.surname@dom
name.surname.extern@dom
My program allows the user to input emails, and to not have to enter "@dom" all the time (it's always the same one), what I've done is allowing the user to write name.surname or name.surname.e and then the script replaces those usernames with @dom or .extern@dom
The problem rises when I have all the mails in different formats stores in a list, and I want them to be filled to standards, so that if I have
["john.doe@dom", "john2.doe", "john3.doe.e","john4.doe.extern@dom"]
it all ends up looking like this
["john.doe@dom", "john2.doe@dom", "john3.doe.extern@dom","john4.doe.extern@dom"]
I have tried with list comprehensions, but all I got was three concatenations:
["%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]
I'm sure there's a better way that does not require me to do 3 list comprehensions and that does not require me to do
for i,v in enumerate(mails):
if "@dom" not in v:
mails[i] = "%s@dom" % v
etc.
python list list-comprehension
python list list-comprehension
asked yesterday
Hamperfait
125110
125110
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
1
down vote
You can use a string's endswith() method to determine what you need to do with the input:
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']
It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!
Edit:
Just for fun, if you insist on a list comprehension:
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']
Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.
add a comment |
up vote
0
down vote
Option without listcomprehensions:
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"
add a comment |
up vote
0
down vote
@Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :
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
Which outputs :
['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']
Hope this helps.
add a comment |
up vote
0
down vote
if you want a one-liner, combine list comprehension with multiple 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
Gives:
['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
add a comment |
up vote
0
down vote
I came with a different solution in the end: Declaring a help function.
def mailfy(mail):
if mail.endswith(".c"):
return mail[:-2] + "@dom"
...
mails = [mailfy(x) for x in mails]
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use a string's endswith() method to determine what you need to do with the input:
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']
It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!
Edit:
Just for fun, if you insist on a list comprehension:
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']
Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.
add a comment |
up vote
1
down vote
You can use a string's endswith() method to determine what you need to do with the input:
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']
It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!
Edit:
Just for fun, if you insist on a list comprehension:
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']
Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use a string's endswith() method to determine what you need to do with the input:
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']
It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!
Edit:
Just for fun, if you insist on a list comprehension:
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']
Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.
You can use a string's endswith() method to determine what you need to do with the input:
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']
It might need more thorough checks to not accept things like @dom right in the middle of the name and whatnot. Hope that helps you out though!
Edit:
Just for fun, if you insist on a list comprehension:
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']
Personally I find list comprehensions are best when they are short and readable, so I would stick with the first option.
edited yesterday
answered yesterday
Green Cell
1,527823
1,527823
add a comment |
add a comment |
up vote
0
down vote
Option without listcomprehensions:
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"
add a comment |
up vote
0
down vote
Option without listcomprehensions:
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"
add a comment |
up vote
0
down vote
up vote
0
down vote
Option without listcomprehensions:
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"
Option without listcomprehensions:
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"
answered yesterday
petruz
431311
431311
add a comment |
add a comment |
up vote
0
down vote
@Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :
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
Which outputs :
['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']
Hope this helps.
add a comment |
up vote
0
down vote
@Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :
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
Which outputs :
['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']
Hope this helps.
add a comment |
up vote
0
down vote
up vote
0
down vote
@Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :
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
Which outputs :
['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']
Hope this helps.
@Green Cell was faster than me and his answer seems to be correct. Here is a list comprehension that does the same thing :
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
Which outputs :
['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']
Hope this helps.
answered yesterday
Alan B.
45110
45110
add a comment |
add a comment |
up vote
0
down vote
if you want a one-liner, combine list comprehension with multiple 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
Gives:
['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
add a comment |
up vote
0
down vote
if you want a one-liner, combine list comprehension with multiple 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
Gives:
['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
add a comment |
up vote
0
down vote
up vote
0
down vote
if you want a one-liner, combine list comprehension with multiple 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
Gives:
['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
if you want a one-liner, combine list comprehension with multiple 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
Gives:
['john.doe@dom', 'john2.doe@dom', 'john3.doe.extern@dom', 'john4.doe.extern@dom']
edited yesterday
answered yesterday
sudonym
1,231824
1,231824
add a comment |
add a comment |
up vote
0
down vote
I came with a different solution in the end: Declaring a help function.
def mailfy(mail):
if mail.endswith(".c"):
return mail[:-2] + "@dom"
...
mails = [mailfy(x) for x in mails]
add a comment |
up vote
0
down vote
I came with a different solution in the end: Declaring a help function.
def mailfy(mail):
if mail.endswith(".c"):
return mail[:-2] + "@dom"
...
mails = [mailfy(x) for x in mails]
add a comment |
up vote
0
down vote
up vote
0
down vote
I came with a different solution in the end: Declaring a help function.
def mailfy(mail):
if mail.endswith(".c"):
return mail[:-2] + "@dom"
...
mails = [mailfy(x) for x in mails]
I came with a different solution in the end: Declaring a help function.
def mailfy(mail):
if mail.endswith(".c"):
return mail[:-2] + "@dom"
...
mails = [mailfy(x) for x in mails]
answered yesterday
Hamperfait
125110
125110
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203425%2fpython-replace-in-list%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password