我正在制作一個需要這個的程式:
我想要python字串中重復字母旁邊的字母表,就像str = "abbbbbcde"我想訪問c字串中的字母表一樣,我該如何訪問它。注意:這個字串是作為用戶輸入的,所以不能直接表示為索引6。
uj5u.com熱心網友回復:
看看這是否對你有幫助。
def find_next_to_repeating(string):
ans = []
last = second_last = None
for i in string:
if second_last and second_last == last and last != i:
ans.append(i)
second_last, last = last, i
return ans
if __name__ == '__main__':
find_next_to_repeating("abbbcde")
uj5u.com熱心網友回復:
我的回答:讓我們拿一個空串列,在其中我們可以從字串中以相同的順序添加不同的元素。然后我們將計算每個元素。時刻計數 >1,我們將列印下一個值。我的代碼:
s=input("enter")
l=[]
for i in s:
if i not in l:
l.append(i) #adding distinct elements
for j in range(len(l)):
x=s.count(l[j])
if x>1 and j!=(len(l)-1):
print(l[j 1]) #if count >0 then print next element
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/310971.html
上一篇:我怎樣才能知道這個方程是否有效?
下一篇:Django:覆寫繼承的欄位選項
