使用正则表达式匹配单词和数字,然后反转单词即可。
示例代码:
import re
def reverse_words_with_numbers(sentence): words = re.findall(r'\w+|\d+', sentence) result = [] for word in words: if word.isalpha(): result.append(word[::-1]) else: result.append(word) return ' '.join(result)
上一篇:不影响客户端的情况下,将一个 C API dll 替换为 C# DLL
下一篇:不影响宽度的情况下在Bootstrap列上设置边距