我想编辑一个字符串后,删除非阿尔法字符,然后移动他们在适当的地方再次。例:
import re
string = input()
alphastring = re.sub('[^0-9a-zA-Z]+', '', string)
alphastring = alphastring[::2]我希望它如下:
awzeklvl."
我试着用不同的解题来解决这个问题,但是没有一个给我正确的输出。我求助于使用不太熟悉的RegEx。任何帮助都将受到极大欢迎。
发布于 2021-10-21 16:07:02
我想不出用regexp做这件事的任何合理方法。只需使用从输入复制到输出的循环,跳过所有其他字母数字字符。
copy_flag = True
string = input()
alphastring = ''
for c in string:
if c.isalnum():
if copy_flag:
alphastring += c
copy_flag = not copy_flag
else:
alphastring += c发布于 2021-10-21 16:21:37
以下是一种方法:
string = "heompuem ykojua'rje awzeklvl."
result, i = "", 0
for c, alph in ((c, c.isalnum()) for c in string)
if not (alph and i%2): # skip odd alpha-numericals
result += c
i += alph # only increment counter for alpha-numericals
result
# "hope you're well."https://stackoverflow.com/questions/69664932
复制相似问题