字符连续出现之间的距离
Distance between consecutive occurrences of a character
我需要编写一个程序来确定给定字符连续出现之间 space 的长度。如果出现相邻,则它们之间的 space 被认为是 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]
我需要编写一个程序来确定给定字符连续出现之间 space 的长度。如果出现相邻,则它们之间的 space 被认为是 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]