简单谈谈Python中的反转字符串问题
作者:hebedich 时间:2022-02-24 11:55:07
按单词反转字符串是一道很常见的面试题。在Python中实现起来非常简单。
def reverse_string_by_word(s):
lst = s.split() # split by blank space by default
return ' '.join(lst[::-1])
s = 'Power of Love'
print reverse_string_by_word(s)
# Love of Power
s = 'Hello World!'
print reverse_string_by_word(s)
# World! Hello
上面的实现其实已经能满足大多数情况,但是并不完美。比如第二个字符串中的感叹号并没有被翻转,而且原字符串中的空格数量也没有保留。(在上面的例子里其实Hello和World之间不止一个空格)
我们期望的结果应该是这样子的。
print reverse_string_by_word(s)
# Expected: !World Hello
要改进上面的方案还不把问题复杂化,推荐使用re模块。你可以查阅re.split() 的官方文档。我们看一下具体例子。
>>> import re
>>> s = 'Hello World!'
>>> re.split(r'\s+', s) # will discard blank spaces
['Hello', 'World!']
>>> re.split(r'(\s+)', s) # will keep spaces as a group
['Hello', ' ', 'World!']
>>> s = '< Welcome to EF.COM! >'
>>> re.split(r'\s+', s) # split by spaces
['<', 'Welcome', 'to', 'EF.COM!', '>']
>>> re.split(r'(\w+)', s) # exactly split by word
['< ', 'Welcome', ' ', 'to', ' ', 'EF', '.', 'COM', '! >']
>>> re.split(r'(\s+|\w+)', s) # split by space and word
['<', ' ', '', 'Welcome', '', ' ', '', 'to', '', ' ', '', 'EF', '.', 'COM', '!', ' ', '>']
>>> ''.join(re.split(r'(\s+|\w+)', s)[::-1])
'> !COM.EF to Welcome <'
>>> ''.join(re.split(r'(\s+)', s)[::-1])
'> EF.COM! to Welcome <'
>>> ''.join(re.split(r'(\w+)', s)[::-1])
'! >COM.EF to Welcome< '
如果你觉得用切片将序列倒序可读性不高,那么其实也可以这样写。
>>> ''.join(reversed(re.split(r'(\s+|\w+)', s)))
'> !COM.EF to Welcome <'
一句话搞定,so easy!
Python翻转字符串(reverse string), 一共包含5种方法, 其中第一种最简单, 即步长为-1, 输出字符串;
方法如下
5种方法的比较:
1. 简单的步长为-1, 即字符串的翻转(常用);
2. 交换前后字母的位置;
3. 递归的方式, 每次输出一个字符;
4. 双端队列, 使用extendleft()函数;
5. 使用for循环, 从左至右输出;
代码:
# -*- coding: utf-8 -*-
#eclipse pydev, python 3.3
#by C.L.Wang
#time: 2014. 4. 11
string = 'abcdef'
def string_reverse1(string):
return string[::-1]
def string_reverse2(string):
t = list(string)
l = len(t)
for i,j in zip(range(l-1, 0, -1), range(l//2)):
t[i], t[j] = t[j], t[i]
return "".join(t)
def string_reverse3(string):
if len(string) <= 1:
return string
return string_reverse3(string[1:]) + string[0]
from collections import deque
def string_reverse4(string):
d = deque()
d.extendleft(string)
return ''.join(d)
def string_reverse5(string):
#return ''.join(string[len(string) - i] for i in range(1, len(string)+1))
return ''.join(string[i] for i in range(len(string)-1, -1, -1))
print(string_reverse1(string))
print(string_reverse2(string))
print(string_reverse3(string))
print(string_reverse4(string))
print(string_reverse5(string))
输出:
fedcba
fedcba
fedcba
fedcba
fedcba
标签:Python,反转字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 自动监控最新邮件并读取的操作
2023-02-04 12:58:51
Python手绘可视化工具cutecharts使用实例
2022-12-25 07:27:54
![](https://img.aspxhome.com/file/2023/3/121403_0s.png)
对Python3+gdal 读取tiff格式数据的实例讲解
2023-03-10 10:21:53
python单例模式获取IP代理的方法详解
2023-11-14 22:22:05
关于python中readlines函数的参数hint的相关知识总结
2023-12-31 02:37:12
![](https://img.aspxhome.com/file/2023/5/63565_0s.png)
python爬虫开发之urllib模块详细使用方法与实例全解
2021-02-24 04:52:42
Python3 批量扫描端口的例子
2021-12-20 05:17:37
如何给 legend 标签设定宽度
2008-07-26 12:18:00
PHP中的闭包function() use() {}使用场景和技巧
2023-06-09 21:54:07
Python中函数的多种格式和使用实例及小技巧
2022-12-18 10:22:32
Python中Tkinter Scrollbar滚动条(窗口滑动条)
2021-11-12 00:22:59
![](https://img.aspxhome.com/file/2023/1/103491_0s.jpg)
CSS的另类拼图___减少HTTP请求
2009-05-28 19:05:00
![](https://img.aspxhome.com/file/UploadPic/20095/28/w200952123340-79s.gif)
MySQL安全性指南 (2)
2010-07-26 13:26:00
使用python检测手机QQ在线状态的脚本代码
2023-03-27 02:12:06
Python THREADING模块中的JOIN()方法深入理解
2021-10-16 16:54:01
Windows自动执行python脚本操作步骤
2023-04-22 15:32:42
![](https://img.aspxhome.com/file/2023/1/101731_0s.jpg)
基于Python爬取京东双十一商品价格曲线
2022-10-22 04:04:42
![](https://img.aspxhome.com/file/2023/7/89097_0s.jpg)
golang组件swagger生成接口文档实践示例
2023-09-19 11:29:39
![](https://img.aspxhome.com/file/2023/3/100793_0s.png)
Mootools 1.2教程(11)——Fx.Morph、Fx选项和Fx事件
2008-12-04 16:03:00
python内置模块OS 实现SHELL端文件处理器
2023-11-24 16:41:14
![](https://img.aspxhome.com/file/2023/2/63882_0s.png)