欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python取出字符串中的偶数_从给定字符串中删除偶数个连续的重复字符

发布时间:2024/10/8 python 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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取出字符串中的偶数_从给定字符串中删除偶数个连续的重复字符的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。