我实际上需要帮助来评估我写的代码正在发生什么。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)。 –