Python – 反转字符串,除了标点符号
给定一个带有标点符号的字符串,执行字符串反转,将标点符号留在原处。
Input : test_str = ‘geeks@#for&%%gee)ks’
Output : skeeg@#rof&%%ske)eg
Explanation : Whole string is reversed, except the punctuations.
Input : test_str = ‘geeks@#for&%%gee)ks’ [ only substring reversal ]
Output : skeeg@#rof&%%eeg)sk
Explanation : Only substrings are reversed.
方法 #1:使用循环 + 堆栈 + 标点符号 + split()
在此,我们使用堆栈来执行字符串反转,检查标点符号,如果当前字符是一个,我们追加它。 split 方法用于处理空格的情况,在反向时需要忽略。
Python3
# Python3 code to demonstrate working of
# Reverse String except punctuations
# Using loop + stack + punctuation + split()
from string import punctuation
# initializing string
test_str = 'geeks# for&%% gee)ks'
# printing original string
print("The original string is : " + str(test_str))
# getting punctuations
punc_set = set(punctuation)
res = []
for sub in test_str.split(' '):
# getting all alphabets
alphas = [chr for chr in sub if chr not in punc_set]
for chr in sub:
# checking for punctuations
if chr not in punc_set:
res.append(alphas.pop())
continue
else:
res.append(chr)
# handling spaces
res.append(' ')
res = "".join(res)
# printing result
print("The Reversed String ignoring punctuation : " + str(res))
Python3
# Python3 code to demonstrate working of
# Reverse String except punctuations
# Using groupby() + isalnum() [ for substring specific reversal ]
from itertools import groupby
# initializing string
test_str = 'geeks# for&%% gee)ks'
# printing original string
print("The original string is : " + str(test_str))
res = ''
# grouping all sections
for ele, sub in groupby(test_str, str.isalnum):
sub = list(sub)
# reversal on alphanumeric occurrence
if ele:
sub = sub[::-1]
# joining all subparts
res += ''.join(sub)
# printing result
print("The Reversed String ignoring punctuation [substring] : " + str(res))
输出
The original string is : geeks#for&%%gee)ks
The Reversed String ignoring punctuation : skeeg#rof&%%ske)eg
方法#2:使用 groupby() + isalnum() [ 用于子串特定的反转]
在此,我们在标点符号之间形成每个子串的分组,并在它们之间反转它们,而不是整体。 isalnum() 用于检查所有字母。
Python3
# Python3 code to demonstrate working of
# Reverse String except punctuations
# Using groupby() + isalnum() [ for substring specific reversal ]
from itertools import groupby
# initializing string
test_str = 'geeks# for&%% gee)ks'
# printing original string
print("The original string is : " + str(test_str))
res = ''
# grouping all sections
for ele, sub in groupby(test_str, str.isalnum):
sub = list(sub)
# reversal on alphanumeric occurrence
if ele:
sub = sub[::-1]
# joining all subparts
res += ''.join(sub)
# printing result
print("The Reversed String ignoring punctuation [substring] : " + str(res))
输出
The original string is : geeks#for&%%gee)ks
The Reversed String ignoring punctuation [substring] : skeeg#rof&%%eeg)sk