华为机考入门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)
相关推荐
专注VB编程开发20年14 小时前
python语法设计、IDE 生态、平台策略、解析器逻辑这四层的矛盾点
开发语言·ide·python
爱睡懒觉的焦糖玛奇朵21 小时前
【从视频到数据集:焦糖玛奇朵的魔法工具使用说明】
人工智能·python·深度学习·学习·算法·yolo·音视频
yangshicong1 天前
第11章:结构化输出与数据提取 —— 让 AI 直接返回你想要的数据格式
数据库·人工智能·redis·python·langchain·ai编程
言之。1 天前
【Python】免费的中文 AI 配音方案
开发语言·人工智能·python
Warson_L1 天前
python dict key详解
python
天天进步20151 天前
Python全栈项目:从零手操一个高性能 API 网关
开发语言·python
安生生申1 天前
使用pygame实现2048
开发语言·python·pygame
徐图图不糊涂1 天前
搭建简易版的Rag系统
python·pycharm
灰灰勇闯IT1 天前
pyasc:用 Python 调用 CANN 的推理能力
开发语言·python
明月_清风1 天前
FastAPI 从入门到实战:3 分钟构建高性能异步 API
后端·python·fastapi