【发布时间】:2022-11-23 07:42:17
【问题描述】:
请我需要在 python3 中编写一个程序,该程序返回一个字符串中的单词数,该字符串的字母仅连续重复 n 次。
- 如果 n=2 "first loop ddd" 代码必须返回 1 [Loop contains 2 o] [d is repeated 3 times in ddd so it wann't be counted]。
我写了很长的代码,但没有得到结果。
words=st.split(" ")
for word in words:
for i in range(1,len(word)-nb+1):
k=word[i:i+nb]
if( k==word[i]*nb and kelma[0]!=word[i-1] and k[-1]!=word[i+nb] ):
nbr=nbr+1
print(word)
break
return nbr
【问题讨论】:
-
你的问题根本不清楚。请尝试改写问题。你期望发生什么?到底发生了什么?请阅读How to Ask 并提供一个minimal reproducible example,我们可以运行它来重现您的问题。
-
我不明白你的例子应该如何返回 2。句子中只有一个单词恰好有两个重复的字母,所以它不应该返回 1,而不是 2?
标签: python