python取出字符串中的偶数_从给定字符串中删除偶数个连续的重复字符
我試圖解決這樣一個問題:我將字符串作為輸入,然后刪除偶數計數的重復字符。在
在輸入:AZXXZYYYDDDYZZZ在
輸出:azzz
你能幫我做這個嗎。在
我的嘗試在刪除重復字符方面效果不錯,但我一直致力于如何刪除偶數個重復字符# Utility function to convert string to list
def toMutable(string):
temp = []
for x in string:
temp.append(x)
return temp
# Utility function to convert string to list
def toString(List):
return ''.join(List)
# Function to remove duplicates in a sorted array
def removeDupsSorted(List):
res_ind = 1
ip_ind = 1
# In place removal of duplicate characters
while ip_ind != len(List):
if List[ip_ind] != List[ip_ind-1]:
List[res_ind] = List[ip_ind]
res_ind += 1
ip_ind+=1
# After above step string is efgkorskkorss.
# Removing extra kkorss after string
string = toString(List[0:res_ind])
return string
# Function removes duplicate characters from the string
# This function work in-place and fills null characters
# in the extra space left
def removeDups(string):
# Convert string to list
List = toMutable(string)
# Sort the character list
List.sort()
# Remove duplicates from sorted
return removeDupsSorted(List)
# Driver program to test the above functions
string = "geeksforgeeks"
print removeDups(string)
總結
以上是生活随笔為你收集整理的python取出字符串中的偶数_从给定字符串中删除偶数个连续的重复字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 菜籽饼能漂白粉一起放吗?
- 下一篇: 如何正确使用电烤炉以烤制肉类?