字符连续出现之间的距离



我需要编写一个程序来确定给定字符连续出现之间的空格长度。如果引用是相邻的,则它们之间的间隔被认为是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]

最新更新