Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50926
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; "'',": 0.07; 'intel': 0.07; 'table.': 0.07; 'string': 0.09; 'character,': 0.09; 'collier': 0.09; 'escape': 0.09; 'literal': 0.09; 'oh,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'translate': 0.10; 'python': 0.11; 'missed': 0.12; "'data'": 0.16; 'brilliant': 0.16; 'buffer.': 0.16; 'merely': 0.16; 'once.': 0.16; 'possible?': 0.16; 'processor,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'side.': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'command': 0.22; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'this:': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'character': 0.29; 'instruction': 0.29; 'code': 0.31; 'that.': 0.31; "d'aprano": 0.31; 'once,': 0.31; 'quotes': 0.31; 'steven': 0.31; 'probably': 0.32; 'front': 0.32; 'regular': 0.32; 'text': 0.33; 'fri,': 0.33; 'could': 0.34; 'johnson': 0.35; 'no,': 0.35; 'but': 0.35; 'vice': 0.36; 'method': 0.36; 'thanks': 0.36; 'should': 0.36; 'searching': 0.37; 'implement': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'expression': 0.60; 'most': 0.60; 'eye': 0.61; 'entire': 0.61; "you're": 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; 'from.': 0.93; 'this!': 0.93; 'wanting': 0.93; '2013': 0.98 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dave Angel <davea@davea.name> |
| Subject | Re: Find and Replace Simplification |
| Date | Fri, 19 Jul 2013 18:45:28 -0400 |
| References | <mailman.4865.1374240179.3114.python-list@python.org> <51e967bb$0$29971$c3e8da3$5496439d@news.astraweb.com> <51E9B338.4060901@Gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | 174.32.174.33 |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 |
| In-Reply-To | <51E9B338.4060901@Gmail.com> |
| 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.4888.1374273947.3114.python-list@python.org> (permalink) |
| Lines | 78 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1374273947 news.xs4all.nl 15864 [2001:888:2000:d::a6]:56320 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50926 |
Show key headers only | View raw
On 07/19/2013 05:44 PM, Devyn Collier Johnson wrote:
>
> 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?
>
Read what you're quoting from. The translate() method does just that.
And maketrans() is the way to build a translate table.
On an Intel processor, the xlat instruction does a translate for one
character, and adding a REP in front of it does it for an entire buffer.
No idea if Python takes advantage of that, however.
--
DaveA
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