Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'python': 0.08; 'gmt': 0.09; '>>>': 0.12; 'wrote:': 0.14; "'',": 0.16; '425': 0.16; 'common.': 0.16; 'simplest': 0.16; 'possibly': 0.16; 'cc:addr :python-list': 0.17; 'voting': 0.19; 'guess': 0.19; 'header:In- Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'trying': 0.23; "what's": 0.23; 'code': 0.24; 'string': 0.26; "i'm": 0.27; 'problem': 0.28; 'subject:how': 0.29; 'cc:addr:python.org': 0.30; "client's": 0.30; 'later.': 0.30; 'programmers': 0.31; 'steven': 0.32; 'rule': 0.34; 'change,': 0.35; "d'aprano": 0.35; 'latter': 0.35; 'numbers.': 0.35; 'charset :us-ascii': 0.36; 'requirements': 0.37; 'think': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'requests': 0.39; 'header:Mime-Version:1': 0.39; 'more': 0.60; 'header:Message- Id:1': 0.62; 'received:208': 0.62; 'url:net': 0.63; 'low': 0.73; "'f',": 0.84; 'democracy': 0.84 Date: Fri, 3 Jun 2011 10:58:57 -0400 From: "D'Arcy J.M. Cain" To: Steven D'Aprano Subject: Re: how to avoid leading white spaces In-Reply-To: <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> <94ph22FrhvU5@mid.individual.net> <4de8eef1$0$29996$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Sylpheed 3.1.0 (GTK+ 2.24.4; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307113146 news.xs4all.nl 49180 [::ffff:82.94.164.166]:45164 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6948 On 03 Jun 2011 14:25:53 GMT Steven D'Aprano wrote: > source.replace(",", " ").split(" ") I would do; source.replace(",", " ").split() > [steve@sylar ~]$ python -m timeit -s "source = 'a b c,d,e,f,g h i j k'" What if the string is 'a b c, d, e,f,g h i j k'? >>> source.replace(",", " ").split(" ") ['a', 'b', 'c', '', 'd', '', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] >>> source.replace(",", " ").split() ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] Of course, it may be that the former is what you want but I think that the latter would be more common. > There's no need to use a regex just because you think that you *might*, > someday, possibly need a regex. That's just silly. If and when > requirements change, then use a regex. Until then, write the simplest > code that will solve the problem you have to solve now, not the problem > you think you might have to solve later. I'm not sure if this should be rule #1 for programmers but it definitely needs to be one of the very low numbers. Trying to guess the client's future requests is always a losing game. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.