从字符串中删除连续的子字符串而不导入任何包
remove consecutive substrings from a string without importing any packages
我想删除 连续的“a”子字符串 并将它们替换为字符串 中的 一个“a”不导入任何包。
例如,我想从 aaabbcccaaaa
得到 abbccca
。
有什么建议么?
谢谢
使用循环有什么问题?
oldstring = 'aaabbcccaaaa'
# Initialise the first character as the same as the initial string
# as this will always be the same.
newstring = oldstring[0]
# Loop through each character starting at the second character
# check if the preceding character is an a, if it isn't add it to
# the new string. If it is an a then check if the current character
# is an a too. If the current character isn't an a then add it to
# the new string.
for i in range(1, len(oldstring)):
if oldstring[i-1] != 'a':
newstring += oldstring[i]
else:
if oldstring[i] != 'a':
newstring += oldstring[i]
print(newstring)
此方法将从您的字符串中删除确定的重复字符:
def remove_dulicated_char(string, char):
new_s = ""
prev = ""
for c in string:
if len(new_s) == 0:
new_s += c
prev = c
if c == prev and c == char:
continue
else:
new_s += c
prev = c
return new_s
print(remove_dulicated_char("aaabbcccaaaa", "a"))
使用 python 正则表达式就可以了。
如果你不知道正则表达式。他们是非常强大的
这种搭配
重新导入
str = 'aaabbcccaaaa'
print(re.sub('a+', 'a', str))
您可以使用函数以递归方式删除出现的字符串的双精度值,直到只剩下重复字符串的一次出现:
val = 'aaabbcccaaaaaaaaaaa'
def remove_doubles(v):
v = v.replace('aa', 'a')
if 'aa' in v:
v = remove_doubles(v)
if 'aa' in v:
v = remove_doubles(v)
else: return v
else: return v
print(remove_doubles(val))
有很多方法可以做到这一点。这是另一个:
def remove_duplicates(s, x):
t = [s[0]]
for c in s[1:]:
if c != x or t[-1] != x:
t.append(c)
return ''.join(t)
print(remove_duplicates('aaabbcccaaaa', 'a'))
我想删除 连续的“a”子字符串 并将它们替换为字符串 中的 一个“a”不导入任何包。
例如,我想从 aaabbcccaaaa
得到 abbccca
。
有什么建议么?
谢谢
使用循环有什么问题?
oldstring = 'aaabbcccaaaa'
# Initialise the first character as the same as the initial string
# as this will always be the same.
newstring = oldstring[0]
# Loop through each character starting at the second character
# check if the preceding character is an a, if it isn't add it to
# the new string. If it is an a then check if the current character
# is an a too. If the current character isn't an a then add it to
# the new string.
for i in range(1, len(oldstring)):
if oldstring[i-1] != 'a':
newstring += oldstring[i]
else:
if oldstring[i] != 'a':
newstring += oldstring[i]
print(newstring)
此方法将从您的字符串中删除确定的重复字符:
def remove_dulicated_char(string, char):
new_s = ""
prev = ""
for c in string:
if len(new_s) == 0:
new_s += c
prev = c
if c == prev and c == char:
continue
else:
new_s += c
prev = c
return new_s
print(remove_dulicated_char("aaabbcccaaaa", "a"))
使用 python 正则表达式就可以了。 如果你不知道正则表达式。他们是非常强大的 这种搭配
重新导入
str = 'aaabbcccaaaa'
print(re.sub('a+', 'a', str))
您可以使用函数以递归方式删除出现的字符串的双精度值,直到只剩下重复字符串的一次出现:
val = 'aaabbcccaaaaaaaaaaa'
def remove_doubles(v):
v = v.replace('aa', 'a')
if 'aa' in v:
v = remove_doubles(v)
if 'aa' in v:
v = remove_doubles(v)
else: return v
else: return v
print(remove_doubles(val))
有很多方法可以做到这一点。这是另一个:
def remove_duplicates(s, x):
t = [s[0]]
for c in s[1:]:
if c != x or t[-1] != x:
t.append(c)
return ''.join(t)
print(remove_duplicates('aaabbcccaaaa', 'a'))