python - Python:获取对之间的距离
问题描述
在我们的数据集中,我们有大量的序列,例如“aismeorisityou”,我们希望得到两个相邻对之间的距离。所以在这种情况下,在两个 'is' 之间还有 6 个其他字母。解决这个问题的最佳方法是什么?
这是我们所得到的..
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
输出应该是距离,6。
解决方案
您将需要第二个内部循环:
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))
返回:
is
separated by: 6
我希望它有帮助!
推荐阅读
- python - 为什么在将漂亮的数据切片转换为 png 图像时会得到完全黑色的标签?
- c++ - 如何移动变体的值
变成一个变种 ? - django - 尽管有 --schedulers 标志,但 django-celery-beat 的权限被拒绝错误
- opengl - 为什么统一变量在 GLSL 中不起作用?
- swift - 导航栏的标题文本属性不起作用。迅速
- php - 使用 Facebook 登录停止在 Laravel 6 中处理回调方法
- typescript - 如何期望字符串以指定的变量开头
- python - 错误:安装 pyaudio 时命令出错,退出状态为 1
- python-3.x - CVXPY 和 CPLEX - 这个属性错误是关于什么的?
- xcode - pod install 错误:给定 podspec `Yoga` 的名称与预期的 `yoga` 不匹配