Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38813

Re: string.replace doesn't removes ":"

X-Received by 10.224.178.207 with SMTP id bn15mr12619163qab.1.1360739414166; Tue, 12 Feb 2013 23:10:14 -0800 (PST)
MIME-Version 1.0
X-Received by 10.49.12.97 with SMTP id x1mr1371472qeb.25.1360739414140; Tue, 12 Feb 2013 23:10:14 -0800 (PST)
Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!p13no14792823qai.0!news-out.google.com!k2ni26867qap.0!nntp.google.com!p13no14792816qai.0!postnews.google.com!e10g2000vbv.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 12 Feb 2013 23:10:14 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info e10g2000vbv.googlegroups.com; posting-host=178.198.163.217; posting-account=ung4FAoAAAC46zhHJ0Nsnuox7M5gDvs_
NNTP-Posting-Host 178.198.163.217
References <mailman.1536.1360407866.2939.python-list@python.org> <92122dd3-ac2e-4407-87ed-0b250ed9c8b9@googlegroups.com> <mailman.1738.1360733201.2939.python-list@python.org>
User-Agent G2/1.0
X-HTTP-UserAgent Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0,gzip(gfe)
Message-ID <25d2297a-6fee-4ef5-bcbd-e26a28cf6ce4@e10g2000vbv.googlegroups.com> (permalink)
Subject Re: string.replace doesn't removes ":"
From jmfauth <wxjmfauth@gmail.com>
Injection-Date Wed, 13 Feb 2013 07:10:14 +0000
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:38813

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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