简体   繁体   English

Python:获取对之间的距离

[英]Python: get the distance between pairs

In our datasets we have big sets of sequences eg "aismeorisityou" which we like to get the distance between the two adjacent pairs.在我们的数据集中,我们有大量的序列,例如“aismeorisityou”,我们希望得到两个相邻对之间的距离。 So in this case between the two 'is's there is 6 other letters.所以在这种情况下,在两个 'is' 之间还有 6 个其他字母。 What's the best way to go about this? go 关于这个的最佳方法是什么?

This is as far as we got..这是我们所得到的..

count = 0
for i in range(1, len(x)):
    if x[i] == x[i-1]:
        # True if there are pairs - now count the distance
return None

The output should be the distance, 6. output 应该是距离,6。

You'll need a second inner loop:您将需要第二个内部循环:

x= 'aismeorisityou'
for i in range(1, len(x)):
    for j in range(i+1, len(x)-1):
        if x[i] == x[j] and x[i+1]==x[j+1]:
            print(x[i]+x[i+1])
            print('separated by: ' + str(j-i))

returns:返回:

is
separated by: 6

I hope it helps!我希望它有帮助!

If the sequences are strings, as your example: "aismeorisityou"如果序列是字符串,例如:“aismeorisityou”

s = 'aismeorisityou'

you can String find (or index) the Substring 'is', and then return both of them.您可以字符串查找(或索引)Substring 'is',然后返回它们。

>>> s.index('is')
1
>>> s.rindex('is')
7
>>> s.find('is')
1
>>> s.rfind('is')
7
>>> 

Write a def, then return the spaces between.写一个def,然后返回之间的空格。

However, what you find with the docs:但是,您在文档中发现的内容:

 |  rfind(...)
 |      S.rfind(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Return -1 on failure.
 |  
 |  rindex(...)
 |      S.rindex(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Raises ValueError when the substring is not found.

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

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