字符连续出现之间的距离

问题描述

我需要编写一个程序来确定给定字符连续出现之间的空格长度。如果出现是相邻的,它们之间的空间被认为是 1。

我只能在两个符号之间计算

text = input ('enter text')
find = input ('enter a search character')
x = text.find(find)

y = text.rfind(find)
   
dist = text.rfind(find) - text.find(find)
print(dist)

解决方法

此代码将将距离输出到列表中

text = "Maybe it's in the gutter,where I left my lover. What an expensive fate."
find = input('Enter a search character')

spaces_list = [len(x) + 1 for x in text.split(find)[1:-1]]
print(spaces_list)

输出

>>> Enter a search character
>>> t

>>> [7,6,1,16,15,17]