华为机考入门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)
相关推荐
u***32434 小时前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
青瓷程序设计6 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95276 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z7 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu7 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟9 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***72139 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄9 小时前
1、python基础语法
开发语言·python
ituff10 小时前
微软认证考试又免费了
后端·python·flask
梁正雄11 小时前
2、Python流程控制
开发语言·python