| Hosted by CoCalc | Download
import re def multiple_replace(txt, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(0)] return rx.sub(one_xlat, txt) txt = "Larry Wall is the creator of Perl" adict = { "Larry Wall" : "Guido van Rossum", "creator" : "Benevolent Dictator for Life", "Perl" : "Python", } print(multiple_replace(txt, adict))
Error in lines 13-13 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 873, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 5, in multiple_replace File "", line 4, in one_xlat IndexError: no such group
%python import re def multiple_replace(txt, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(0)] return rx.sub(one_xlat, txt) txt = "Larry Wall is the creator of Perl" adict = { "Larry Wall" : "Guido van Rossum", "creator" : "Benevolent Dictator for Life", "Perl" : "Python", } print(multiple_replace(txt, adict))
Guido van Rossum is the Benevolent Dictator for Life of Python
%python import re def multiple_replace(txt, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(Integer(0))] return rx.sub(one_xlat, txt) txt = "Larry Wall is the creator of Perl" adict = { "Larry Wall" : "Guido van Rossum", "creator" : "Benevolent Dictator for Life", "Perl" : "Python", } print(multiple_replace(txt, adict))
Error in lines 13-13 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 873, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 5, in multiple_replace File "", line 4, in one_xlat IndexError: no such group
import re def multiple_replace(txt, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(int(0))] return rx.sub(one_xlat, txt) txt = "Larry Wall is the creator of Perl" adict = { "Larry Wall" : "Guido van Rossum", "creator" : "Benevolent Dictator for Life", "Perl" : "Python", } print(multiple_replace(txt, adict))
Guido van Rossum is the Benevolent Dictator for Life of Python
import re def multiple_replace(txt, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(0r)] return rx.sub(one_xlat, txt) txt = "Larry Wall is the creator of Perl" adict = { "Larry Wall" : "Guido van Rossum", "creator" : "Benevolent Dictator for Life", "Perl" : "Python", } print(multiple_replace(txt, adict))
Guido van Rossum is the Benevolent Dictator for Life of Python