python - How to replace a character within a string in a list? - Stack Overflow
A list of string replacements in Python - Stack Overflow
Making str.replace() accept lists - Ideas - Discussions on Python.org
python - Replace in strings of list - Stack Overflow
I'm trying to replace the same characters in a list of strings but nothing is happening?
list = ["stacy", "tracy", "kacy"]
newlist=[]
for x in list:
x.replace("cy","fi")
newlist.append(x)
print(newlist)What's wrong here?
Looks like a good opportunity to use a loop:
mapping = { 'A':'1', 'B':'2', 'C':'3', 'D':'4', 'E':'5'}
for k, v in mapping.iteritems():
my_string = my_string.replace(k, v)
A faster approach if you don't mind the parentheses would be:
mapping = [ ('A', '1'), ('B', '2'), ('C', '3'), ('D', '4'), ('E', '5') ]
for k, v in mapping:
my_string = my_string.replace(k, v)
You can easily use string.maketrans() to create the mapping string to pass to str.translate():
import string
trans = string.maketrans("ABCDE","12345")
my_string = my_string.translate(trans)
>>> a = ["abcd:+;", "(l&'kka)"]
>>> a = [re.sub('[\(\)&\':+]', '', x) for x in a]
>>> a
['abcd;', 'lkka']
>>>
for index,x in enumerate(a):
a[index] = re.sub('[\(\)&\':+]', '', x)
Your changing the value but not updating your list. enumerate is function that return tuple (index,value) for each item of list
I want to replace strings in a list, with characters of another list, but I don't know how to do it, for example I have an input: 'a__l_', and I input the letters that I want to replace the '_'s with, such as input: 'a, b,c,d,e' and I want to replace the special character with the letter, how do I do that?