Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50922
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <devyncjohnson@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.057 |
| X-Spam-Evidence | '*H*': 0.89; '*S*': 0.00; "'',": 0.07; 'string': 0.09; 'collier': 0.09; 'escape': 0.09; 'literal': 0.09; 'oh,': 0.09; 'translate': 0.10; 'python': 0.11; 'missed': 0.12; "'data'": 0.16; 'brilliant': 0.16; 'merely': 0.16; 'once.': 0.16; 'possible?': 0.16; 'side.': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'command': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'replace': 0.24; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'code': 0.31; 'received:10.0.0': 0.31; 'that.': 0.31; "d'aprano": 0.31; 'once,': 0.31; 'quotes': 0.31; 'steven': 0.31; 'probably': 0.32; 'regular': 0.32; 'text': 0.33; 'fri,': 0.33; 'could': 0.34; 'johnson': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'vice': 0.36; 'method': 0.36; 'thanks': 0.36; 'received:10.0': 0.36; 'should': 0.36; 'searching': 0.37; 'received:10': 0.37; 'implement': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'most': 0.60; 'eye': 0.61; "you'll": 0.62; 'more': 0.64; 'believe': 0.68; 'to,': 0.72; 'jul': 0.74; 'hand': 0.80; 'you:': 0.81; 'faster.': 0.84; 'replacements': 0.84; 'subject:skip:S 10': 0.84; 'better!': 0.91; 'do:': 0.91; 'error;': 0.91; 'this!': 0.93; 'wanting': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=cx7VHaa/yg9dimY8scvsSOPQjg1E5c3oOR5Loydxo74=; b=isT0tWo5E4dpVAw5bYhbulLVTiWyh4SmT6l60HCML+YyDdite5kFfZqZ3QGdOiE7AH pd44rzVOld87iVeqOu9ulYN+ucs1VA8m659mGbC4Pv4zSioccyT19TiW+1FSAuBWGHke ChtsyVkLdyxPtDCNlenBqRZB6eOem95E8Hb4+620bBrvQyysm7EGYii135ZnxNgntaLh J7n4hghkBNdcgyNkYsDDClzbOVfP+8M2bzJ3OBAusKvhtBBHlNHRvph1YRlYmk3IzTvo KshAMvY+r8Q2q70MUkLKLimT5zwZ6XqErNrtDjCMaVj8azpX8nBi8gvZuBzdmtoEtz8h LZvw== |
| X-Received | by 10.50.22.72 with SMTP id b8mr13484289igf.17.1374270267878; Fri, 19 Jul 2013 14:44:27 -0700 (PDT) |
| Date | Fri, 19 Jul 2013 17:44:24 -0400 |
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 |
| MIME-Version | 1.0 |
| To | Python Mailing List <python-list@python.org> |
| Subject | Re: Find and Replace Simplification |
| References | <mailman.4865.1374240179.3114.python-list@python.org> <51e967bb$0$29971$c3e8da3$5496439d@news.astraweb.com> |
| In-Reply-To | <51e967bb$0$29971$c3e8da3$5496439d@news.astraweb.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4884.1374270271.3114.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1374270271 news.xs4all.nl 15947 [2001:888:2000:d::a6]:40302 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50922 |
Show key headers only | View raw
On 07/19/2013 12:22 PM, Steven D'Aprano wrote:
> On Fri, 19 Jul 2013 09:22:48 -0400, Devyn Collier Johnson wrote:
>
>> I have some code that I want to simplify. I know that a for-loop would
>> work well, but can I make re.sub perform all of the below tasks at once,
>> or can I write this in a way that is more efficient than using a
>> for-loop?
>>
>> DATA = re.sub(',', '', 'DATA')
>> DATA = re.sub('\'', '', 'DATA')
>> DATA = re.sub('(', '', 'DATA')
>> DATA = re.sub(')', '', 'DATA')
>
> I don't think you intended to put DATA in quotes on the right hand side.
> That makes it literally the string D A T A, so all those replacements are
> no-ops, and you could simplify it to:
>
> DATA = 'DATA'
>
> But that's probably not what you wanted.
>
> My prediction is that this will be by far the most efficient way to do
> what you are trying to do:
>
> py> DATA = "Hello, 'World'()"
> py> DATA.translate(dict.fromkeys(ord(c) for c in ",'()"))
> 'Hello World'
>
> That's in Python 3 -- in Python 2, using translate will still probably be
> the fastest, but you'll need to call it like this:
>
> import string
> DATA.translate(string.maketrans("", ""), ",'()")
>
> I also expect that the string replace() method will be second fastest,
> and re.sub will be the slowest, by a very long way.
>
> As a general rule, you should avoiding using regexes unless the text you
> are searching for actually contains a regular expression of some kind. If
> it's merely a literal character or substring, standard string methods
> will probably be faster.
>
>
> Oh, and a tip for you:
>
> - don't escape quotes unless you don't need to, use the other quote.
>
> s = '\'' # No, don't do this!
> s = "'" # Better!
>
> and vice versa.
>
>
>
>
Thanks for finding that error; DATA should not be in quotes. I cannot
believe I missed that. Good eye Steven!
Using the replace command is a brilliant idea; I will implement that
where ever I can. I am wanting to perform all of the replaces at once.
Is that possible?
Mahalo,
DCJ
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Find and Replace Simplification Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 09:22 -0400
Re: Find and Replace Simplification Novocastrian_Nomad <gregory.j.baker@gmail.com> - 2013-07-19 06:38 -0700
Re: Find and Replace Simplification John Gordon <gordon@panix.com> - 2013-07-19 14:28 +0000
Re: Find and Replace Simplification Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-19 16:22 +0000
Re: Find and Replace Simplification Serhiy Storchaka <storchaka@gmail.com> - 2013-07-19 20:29 +0300
Re: Find and Replace Simplification Skip Montanaro <skip@pobox.com> - 2013-07-19 13:08 -0500
Re: Find and Replace Simplification Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 17:44 -0400
Re: Find and Replace Simplification Dave Angel <davea@davea.name> - 2013-07-19 18:45 -0400
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-20 12:16 +0100
Re: Find and Replace Simplification Serhiy Storchaka <storchaka@gmail.com> - 2013-07-20 14:48 +0300
Re: Find and Replace Simplification Serhiy Storchaka <storchaka@gmail.com> - 2013-07-20 14:57 +0300
Re: Find and Replace Simplification Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:41 -0400
Re: Find and Replace Simplification Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:50 -0400
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-20 18:03 +0100
Re: Find and Replace Simplification Dave Angel <davea@davea.name> - 2013-07-20 14:04 -0400
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-20 19:37 +0100
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-20 19:41 +0100
Re: Find and Replace Simplification Dave Angel <davea@davea.name> - 2013-07-20 17:56 -0400
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-20 23:33 +0100
Re: Find and Replace Simplification Serhiy Storchaka <storchaka@gmail.com> - 2013-07-21 10:44 +0300
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-21 12:29 +0100
Re: Find and Replace Simplification Serhiy Storchaka <storchaka@gmail.com> - 2013-07-21 15:28 +0300
Re: Find and Replace Simplification Joshua Landau <joshua@landau.ws> - 2013-07-21 13:49 +0100
csiph-web