I have a list of list input- old_list:
[['hi.','pt','patient','med',...], ['.','md','pt','md',...]...]
my desired output – new_list:
[['hi',' ',' ','medication',...], [' ', 'medication', ' ',...]...]
and I have tried
1.
adict = {".": " ",
"patient": " ",
"bptb": " ",
"bmedb":"medication"}
for key, value in adict.items():
new_list= [[re.sub(key, value, e) for e in d] for d in old_list]
replacements = [('.', ""),
("patient"," "),
("bptb", " "),
("bmedb","medication")]
for old, new in replacements:
new_list=[]
new_list= [[re.sub(old, new, e) for e in d] for d in old_list]
- and
replace(new_list, old, new) for ...
but none of them works, the output is the same as the original old_list. Any suggestions? Thanks!
Advertisement
Answer
- You need to use output of each iteration as input for a next iteration, i.e.
in new_listinstead ofin old_list. And of course to initialize the variable before loop:new_list = old_list. - Regex patterns should have
r-prefix. - As mentioned in comments, avoid naming variables with built-in names like
dictandlist.
import re
patterns = {
r".": " ",
r"patient": " ",
r"bptb": " ",
r"bmedb": "medication",
}
old_list = [['hi.', 'pt', 'patient', 'start med end'], ['.', 'md', 'pt', 'md']]
new_list = old_list
for key, value in patterns.items():
new_list = [[re.sub(key, value, e) for e in d] for d in new_list]
print(old_list)
print(new_list)