我實際上需要幫助來評估我寫的代碼正在發生什麼。Python 3.xx - 從字符串中刪除連續的數字/字母
這意味着這樣的功能:
input: remove_duple('WubbaLubbaDubDub')
output: 'WubaLubaDubDub'
另一個例子:
input: remove_duple('aabbccdd')
output: 'abcd'
我還是一個初學者,我想知道這兩個有什麼錯我的代碼和更容易方法來做到這一點。 (有些情況下,是我努力想象發生了什麼事的一部分,調試它的一些代碼行)
def remove_duple(string):
to_test = list(string)
print (to_test)
icount = 0
dcount = icount + 1
for char in to_test:
if to_test[icount] == to_test[dcount]:
del to_test[dcount]
print ('duplicate deleted')
print (to_test)
icount += 1
elif to_test[icount] != to_test[dcount]:
print ('no duplicated deleted')
print (to_test)
icount += 1
print ("".join(to_test))
看看它看起來有多簡單[用正則表達式](https://ideone.com/BpT3NE)。 –