简体   繁体   English

查找两个相同弦之间的距离

[英]Find distance between two same strings

Let's say I have following string: ABCyuioppsfsABCsfrsfsfsaABCfds 假设我有以下字符串:ABCyuioppsfsABCsfrsfsfsaABCfds

How can I quickly find the distance between first string "ABC" and all other "ABC" strings? 如何快速找到第一个字符串“ ABC”和所有其他“ ABC”字符串之间的距离?

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. 第一个“ ABC”字符串从字符串的第一个位置开始,第二个“ ABC”字符串从字符串的第13个位置开始,第三个字符串从字符串的第25个位置开始。 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] [0,9,9]

This prints out the distance between each 'ABC' . 打印出每个'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] [1,13,25]

You can use re.finditer in a list comprehension for this. 您可以在列表理解中使用re.finditer来实现。 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: 您可以找到每个ABC所有索引,然后从其余的索引中减去第一个:

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). 如果您正在考虑“之间的距离”,则必须指定该距离是在“ ABC”的每个开始位置之间还是在它们之间的字符数(“ ABC”字符串本身除外)之间。 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. 似乎您正在寻找基于1的索引。 (indexes in Python lists are zero-based). (Python列表中的索引从零开始)。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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