华为机考入门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)
相关推荐
A__tao11 小时前
JSON 转 Java 实体类工具(支持嵌套与注释解析)
java·python·json
zhouwy11311 小时前
Python 基础语法笔记:从入门到进阶的系统学习
python
高洁0112 小时前
工程科研中的AI应用:结构力学分析技巧
python·深度学习·机器学习·数据挖掘·知识图谱
大邳草民12 小时前
Python 爬虫:从 HTTP 请求到接口分析
笔记·爬虫·python
众创岛12 小时前
Playwright 元素定位
python·自动化
艾派森12 小时前
深度学习实战-基于EfficientNetB5的家禽鸡病图像分类识别模型
人工智能·python·深度学习·神经网络·分类
心静财富之门12 小时前
Django 超详细初级教程(零基础可学)
python·django
bucenggaibian12 小时前
Nearoh:9年开发者从零造语言,Python的简洁+C的性能
c语言·python·开发者·编程语言·nearoh
小小测试开发12 小时前
EasyOCR用法全攻略:Python开源OCR工具快速上手,图文识别零门槛
python·开源·ocr
用户239356104805812 小时前
DeepTutor源码安装与配置
python