On 13 fév, 06:26, Rick Johnson <rantingrickjohn...@gmail.com> wrote: > On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote: > > ============================================================ > > REFERENCES: > > ============================================================ > > [1]: Should string.replace handle list, tuple and dict > > arguments in addition to strings? > > > py> string.replace(('a', 'b', 'c'), 'abcdefgabc') > > 'defg' > > [...] > > And here is a fine example of how a "global function architecture" can > seriously warp your mind! Let me try that again! > > Hypothetical Examples: > > py> 'abcdefgabc'.replace(('a', 'b', 'c'), "") > 'defg' > py> 'abcdefgabc'.replace(['a', 'b', 'c'], "") > 'defg' > py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'}) > 'A2CdefgA2C' > > Or, an alternative to passing dict where both old and new arguments accept > the sequence: > > py> d = {'a':'A', 'b':'2', 'c':'C'} > py> 'abcdefgabc'.replace(d.keys(), d.values()) > 'A2CdefgA2C' > > Nice thing about dict is you can control both sub-string and > replacement-string on a case-by-case basis. But there is going to be a need > to apply a single replacement string to a sequence of substrings; like the > null string example provided by the OP. > > (hopefully there's no mistakes this time)
-------- >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'} >>> 'abcdefgabc'.translate(d) 'A2CdefgA2C' >>> >>> >>> def jmTranslate(s, table): ... table = {ord(k):table[k] for k in table} ... return s.translate(table) ... >>> d = {'a': 'A', 'b': '2', 'c': 'C'} >>> jmTranslate('abcdefgabc', d) 'A2CdefgA2C' >>> d = {'a': None, 'b': None, 'c': None} >>> jmTranslate('abcdefgabc', d) 'defg' >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'} >>> jmTranslate('abcdefgabc', d) '€€€€€€€€€€€€€defg€€€€€€€€€€€€€' >>> jmf -- http://mail.python.org/mailman/listinfo/python-list