Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38813
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-02-12 23:10 -0800 |
| References | <mailman.1536.1360407866.2939.python-list@python.org> <92122dd3-ac2e-4407-87ed-0b250ed9c8b9@googlegroups.com> <mailman.1738.1360733201.2939.python-list@python.org> |
| Message-ID | <25d2297a-6fee-4ef5-bcbd-e26a28cf6ce4@e10g2000vbv.googlegroups.com> (permalink) |
| Subject | Re: string.replace doesn't removes ":" |
| From | jmfauth <wxjmfauth@gmail.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
string.replace doesn't removes ":" Joshua Robinson <shooki.robinson@gmail.com> - 2013-02-09 06:04 -0500
Re: string.replace doesn't removes ":" Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-02-10 11:36 +0100
Re: string.replace doesn't removes ":" vduncan80@gmail.com - 2013-02-12 07:14 -0800
Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800
Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-12 23:10 -0800
Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:34 -0800
Re: string.replace doesn't removes ":" Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-13 16:55 +0000
Re: string.replace doesn't removes ":" Walter Hurry <walterhurry@lavabit.com> - 2013-02-13 18:16 +0000
Re: string.replace doesn't removes ":" 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-13 12:24 -0800
Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-14 00:02 -0800
Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800
csiph-web