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

2013-01-06 Thread Roy Smith
In article roy-103d43.15470305012...@news.panix.com, Roy Smith r...@panix.com wrote: It's rare to find applications these days that are truly CPU bound. Once you've used some reasonable algorithm, i.e. not done anything in O(n^2) that could have been done in O(n) or O(n log n), you will

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

2013-01-06 Thread Mitya Sirenef
On 01/06/2013 01:32 AM, Mitya Sirenef wrote: On 01/05/2013 03:35 AM, Sia wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign,

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

2013-01-06 Thread Steven D'Aprano
On Sun, 06 Jan 2013 12:28:55 -0500, Roy Smith wrote: I've been doing some log analysis. It's been taking a grovelingly long time, so I decided to fire up the profiler and see what's taking so long. I had a pretty good idea of where the ONLY TWO POSSIBLE hotspots might be (looking up IP

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

2013-01-06 Thread Nick Mellor
Hi Sia, Here's another variation. It's within my tolerance for readability :-) and also quick, if that's an issue. It does 100,000 of your longer string in a couple of seconds on my venerable laptop. It handles only single-digit numbers. For multi-digit, I'd be inclined to have a look at

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

2013-01-06 Thread Nick Mellor
Hi Sia, Find a multi-digit method in this version: from string import maketrans from itertools import takewhile def is_digit(s): return s.isdigit() class redux: def __init__(self): intab = '+-' outtab = ' ' self.trantab = maketrans(intab, outtab) def

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

2013-01-06 Thread Nick Mellor
Note that the multi-line version above tolerates missing digits: if the number is missing after the '+/-' it doesn't skip any letters. Brief explanation of the multi-digit version: +/- are converted to spaces and used to split the string into sections. The split process effectively swallows

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

2013-01-06 Thread Nick Mellor
Oops! You skip 2 + 11 characters, 2 digits in 12 and 11 letters following. And incidentally in: should read: You skip 2 + 11 characters, 2 digits in 11 and 11 letters following. And incidentally in: N On Saturday, 5 January 2013 19:35:26 UTC+11, Sia wrote: I have strings such as:

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

2013-01-06 Thread John Ladasky
On Saturday, January 5, 2013 12:35:26 AM UTC-8, Sia wrote: I have strings such as: tA.-2AG.-2AG,-2ag .+3ACG.+5CAACG.+3ACG.+3ACG Just curious, do these strings represent DNA sequences? -- http://mail.python.org/mailman/listinfo/python-list

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

2013-01-06 Thread Nick Mellor
Hi Sia, Thanks for the problem! I hope you find these examples understandable. Below, find an inflexible but fairly fast single-digit method and a slower (but still respectable) multi-digit method that copes with entirely absent digits after +/- and multi-digit skips such as 12 or 37 or 186

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

2013-01-05 Thread Sia
I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters after that. So the two strings above become:

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

2013-01-05 Thread Frank Millman
On 05/01/2013 10:35, Sia wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters after that.

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

2013-01-05 Thread Chris Angelico
On Sat, Jan 5, 2013 at 7:35 PM, Sia hossein.asghar...@gmail.com wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number

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

2013-01-05 Thread Roy Smith
In article e480480d-f3b4-4491-969c-7d1843bf9...@googlegroups.com, Sia hossein.asghar...@gmail.com wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG Some kind of DNA binding site? A couple of questions. Are the numbers always single digits? How much data is

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

2013-01-05 Thread Roy Smith
In article mailman.109.1357378077.2939.python-l...@python.org, Chris Angelico ros...@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

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

2013-01-05 Thread Chris Angelico
On Sun, Jan 6, 2013 at 1:30 AM, Roy Smith r...@panix.com wrote: In article mailman.109.1357378077.2939.python-l...@python.org, Chris Angelico ros...@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

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

2013-01-05 Thread Roy Smith
In article mailman.120.1357397255.2939.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: On Sun, Jan 6, 2013 at 1:30 AM, Roy Smith r...@panix.com wrote: In article mailman.109.1357378077.2939.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: result =

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

2013-01-05 Thread Chris Angelico
On Sun, Jan 6, 2013 at 2:03 AM, Roy Smith r...@panix.com wrote: That's why I chose to split this where I did. It was where the scan direction changed. Ah, good point. In any case, this is a fairly simple and clear way of doing things; it may or may not run faster than the explicit state

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

2013-01-05 Thread Roy Smith
In article mailman.121.1357398573.2939.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: it may or may not run faster than the explicit state machine, Hmmm, hard to say. Both are O(n), but yours makes several more copies of the data than mine (the string addition, the replace(),

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

2013-01-05 Thread Chris Angelico
On Sun, Jan 6, 2013 at 2:38 AM, Roy Smith r...@panix.com wrote: In article mailman.121.1357398573.2939.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: it may or may not run faster than the explicit state machine, You got me by a factor of 3 or 4. Not bad. You miss my point,

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

2013-01-05 Thread Tim Chase
On 01/05/13 02:35, Sia wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters after that.

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

2013-01-05 Thread Tim Chase
On 01/05/13 11:24, Tim Chase wrote: I don't know how this version times out: import re r = re.compile(r[-+](\d+)([^-+]*)) def modify(m): result = m.group(2)[int(m.group(1)):] return result Doh, I intended to change this after testing, making it just returm

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

2013-01-05 Thread Ian Kelly
On Sat, Jan 5, 2013 at 8:57 AM, Chris Angelico ros...@gmail.com wrote: You miss my point, though. I went for simple Pythonic code, and never measured its performance, on the expectation that it's good enough. Written in C, the state machine is probably WAY faster than splitting and then

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

2013-01-05 Thread Chris Angelico
On Sun, Jan 6, 2013 at 7:04 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Sat, Jan 5, 2013 at 8:57 AM, Chris Angelico ros...@gmail.com wrote: You miss my point, though. I went for simple Pythonic code, and never measured its performance, on the expectation that it's good enough. Written in C,

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

2013-01-05 Thread Roy Smith
In article mailman.142.1357417943.2939.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: On Sun, Jan 6, 2013 at 7:04 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Sat, Jan 5, 2013 at 8:57 AM, Chris Angelico ros...@gmail.com wrote: You miss my point, though. I went for simple

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

2013-01-05 Thread Mitya Sirenef
On 01/05/2013 03:35 AM, Sia wrote: I have strings such as: tA.-2AG.-2AG,-2ag or .+3ACG.+5CAACG.+3ACG.+3ACG The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters