Reputation: 43
Let's say I have following string: ABCyuioppsfsABCsfrsfsfsaABCfds
How can I quickly find the distance between first string "ABC" and all other "ABC" strings?
first "ABC" string starts on 1st position of string, second "ABC" string starts with 13th position of the string and 3rd string starts with 25th position of string. I want to find how to quickly count it
Upvotes: 0
Views: 423
Reputation: 42129
If you're thinking "distance between", you have to specify if the distance is between each beginning position of "ABC" or the number of characters in between them (excluding the "ABC" strings themselves). On the other hand, your examples seem to indicate that you are not looking for distances at all. It would seem you are looking for one-based indexes. (indexes in Python lists are zero-based).
s = "ABCyuioppsfsABCsfrsfsfsaABCfds"
from itertools import accumulate
distance_between_strings = accumulate( len(d)+3*(i>0) for i,d in enumerate(s.split("ABC")[1:-1]) )
print(list(distance_between_strings))
# [9, 21]
distance_between_starts = accumulate(len(d)+3 for d in s.split("ABC")[1:-1])
print(list(distance_between_starts))
# [12, 24]
import re
just_positions = [m.start()+1 for m in re.finditer("ABC",s)]
print(just_positions)
# [1, 13, 25]
Upvotes: 0
Reputation: 451
How about a list comprehension?
A='ABCyuioppsfsABCsfrsfsfsaABCfds'
[len(i) for i in A.split('ABC')][:-1]
[0, 9, 9]
This prints out the distance between each 'ABC'
.
EDIT: Accounting for your post edit:
import itertools
A='ABCyuioppsfsABCsfrsfsfsaABCfds'
li=[len(i)+1 if len(i)==0 else len(i)+len('ABC') for i in A.split('ABC')][:-1]
print(list(itertools.accumulate(li)))
[1,13,25]
Upvotes: 2
Reputation: 26335
You can find all indices of each ABC
, then subtract the first one from the rest:
from re import finditer
abc = "ABCyuioppsfsABCsfrsfsfsaABCfds"
indices = [m.start() for m in finditer('ABC', abc)]
diffs = [x - indices[0] for x in indices[1:]]
print(diffs)
# [12, 24]
Upvotes: 0
Reputation: 92461
You can use re.finditer
in a list comprehension for this. This will also return the first match, which can, of course, ignore or slice off:
>>> import re
>>> s = 'ABCyuioppsfsABCsfrsfsfsaABCfds'
>>> [sub.start() for sub in re.finditer('ABC', s)]
[0, 12, 24]
Upvotes: 1