简体   繁体   中英

Find distance between two same strings

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

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]

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]

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]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM