华为机考入门python3--(31)牛客31-单词倒排

分类:字符串、正则

知识点:

  1. 正则提取所有符合的字符串 words = re.findall(r'a-zA-Z+', sentence)

  2. 列表倒序 words::-1

题目来自【牛客】

python 复制代码
import re  # 导入正则表达式模块

def reverse_words(sentence):
    # 使用正则表达式将句子拆分成单词
    # 如可以将$bo*y gi!r#l提取为['bo', 'y', 'gi', 'r', 'l']
    words = re.findall(r'[a-zA-Z]+', sentence)
    # print(words)
    
    # 对单词列表进行倒序,并用空格连接单词
    reversed_sentence = ' '.join(words[::-1])
    
    return reversed_sentence

# 输入
input_sentence = input()

# 倒排单词并输出结果
result = reverse_words(input_sentence)
print(result)
相关推荐
半亩码田7 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx1980128 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
Python私教8 小时前
签名 URL 刷新导致审批失效?别急着删掉所有 query
python·安全
牧羊人.3338 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python
Zane19948 小时前
类变量与实例变量:一个共享列表引发的线上事故
后端·python
Python私教8 小时前
一次性批准令牌:挡住旧授权误发新版本的工程设计
python·安全
for_ever_love__9 小时前
python基础语法学习: 数据容器
windows·python·学习
aiqianji9 小时前
文风接近真人的AI生成短篇小说软件有哪些?
人工智能·python
Sylvia33.9 小时前
从轮询到推送:足球数据API架构演进与火星数据技术拆解
java·服务器·网络·python·websocket·架构
瑞码空间9 小时前
Python爬虫进阶实战笔记
开发语言·python·计算机·python爬虫