Need a specific sort of string modification. Can someone help?

Roy Smith roy at panix.com
Sat Jan 5 16:03:06 CET 2013


In article <mailman.120.1357397255.2939.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Jan 6, 2013 at 1:30 AM, Roy Smith <roy at panix.com> wrote:
> > In article <mailman.109.1357378077.2939.python-list at python.org>,
> >  Chris Angelico <rosuav at gmail.com> wrote:
> >
> >> result = "".join([x[int(x[0])+1:] for x in 
> >> ("0"+s).replace("-","+").split("+")])
> >
> > That's exceedingly clever.  But bordering on line noise.  At the very
> > least, I would break it up into a couple of lines to make it easier to
> > understand (plus you can print out the intermediate values to see what's
> > going on):
> >
> > chunks = ("0"+s).replace("-","+").split("+")
> > result = "".join([x[int(x[0])+1:] for x in chunks]
> 
> Sure. You can always split a one-liner to taste, doesn't much matter
> where.

Well, there are better and worse places to split things.  Consider these 
three statements:

result = f1().f2().f3().f4().f5()
result = f1(f2.f3(f4().f5()))
result = f1(f2(3(f4(f5()))))

Same number of function calls, but the middle one is clearly the most 
difficult to understand.  The reason is because the scan direction keeps 
changing.  The first one is strictly left-to-right.  The last one is 
strictly inside-to-outside.  The middle one is all over the place.

That's why I chose to split this where I did.  It was where the scan 
direction changed.

Readability matters.



More information about the Python-list mailing list