Python实现字符串反转的几种方法
面试遇到的一个特无聊的问题~~~
要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba"
第一种:使用字符串切片
result = s[::-1]
第二种:使用列表的reverse方法
l = list(s)
l.reverse()
result = "".join(l)
当然下面也行
l = list(s)
result = "".join(l[::-1])
第三种:使用reduce
result = reduce(lambda x,y:y+x,s)
第四种:使用递归函数
def func(s):
if len(s) <1:
return s
return func(s[1:])+s[0]
result = func(s)
第五种:使用栈
def func(s):
l = list(s) #模拟全部入栈
result = ""
while len(l)>0:
result += l.pop() #模拟出栈
return result
result = func(s)
第六种:for循环
def func(s):
result = ""
max_index = len(s)-1
for index,value in enumerate(s):
result += s[max_index-index]
return result
result = func(s)
只能想起来这么多了,还有吗?
Python实现字符串反转的几种方法的更多相关文章
- [转]Python实现字符串反转的几种方法
#第一种:使用字符串切片 result = s[::-1] #第二种:使用列表的reverse方法 l = list(s) l.reverse() result = "".join ...
- [Python]实现字符串倒序的三种方法
a=" 1: print(a[::-1]) 2: b=list(a) b.reverse() print(''.join(b)) 3: c=len(a)-1 str_1=[] while(c ...
- 趣味算法:字符串反转的N种方法(转)
老赵在反对北大青鸟的随笔中提到了数组反转.这的确是一道非常基础的算法题,然而也是一道很不平常的算法题(也许所有的算法深究下去都会很不平常).因为我写着写着,就写出来8种方法……现在我们以字符串的反转为 ...
- Java实现字符串反转的8种方法
/** * */ package com.wsheng.aggregator.algorithm.string; import java.util.Stack; /** * 8 种字符串反转的方法, ...
- Python中字符串拼接的N种方法
python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...
- python中字符串格式化的两种方法
知识点汇总;1-字符串格式化输出方法一: % 1-print('名字是 %s,年龄是%s' % (name ,age)) 2- %s ---字符串-----相当于执行了str() 3- (name , ...
- Java将字符串反转的7种方法
/方法1 递归方法 public static String reverse1(String s) { int length = s.length(); if(length <= 1){ ret ...
- javascript 实现字符串反转的两种方法
第一种方法:利用数组方法 //先split将字串变成单字数组,然后reverse()反转,然后将数组拼接回字串 var str = "abcdef"; str.split(&quo ...
- c++中字符串反转的3种方法
第一种:使用algorithm中的reverse函数 #include <iostream> #include <string> #include <algorithm& ...
随机推荐
- hdu 3480 Division(四边形不等式优化)
Problem Description Little D is really interested in the theorem of sets recently. There’s a problem ...
- pm2自动部署
配置pm2自动部署前,请确保已经能够ssh免密登录服务器. 一.创建ecosystem.json { "apps" : [{ "name" : "HT ...
- Spring security oauth2 client_credentials认证 最简单示例代码
基于spring-boot-2.0.0 1,在pom.xml中添加: <!-- security --> <!-- https://mvnrepository.com/artifac ...
- CentOS7配置iptables防火墙
CentOS 7中默认是firewalld防火墙,如果使用iptables需要先关闭firewalld防火墙(1.关闭防火墙,2.取消开机启动). #关闭firewalld systemctl sto ...
- SEO学习知识
监控流量的工具 百度统计 CNZZ 51LA 谷歌分析工具 如何从平台借流量? 竞价(付费).SEO 关键词定位: 定位人:负责人 将公司的业务全部列出来 选词: 根据定位的关键词选择出我们需要优化 ...
- Python系列之 - 异常处理
python提供的异常处理 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常 ...
- [再寄小读者之数学篇](2014-07-27 $H^{-1}$ 中的有界集与弱收敛极限)
设 $H^{-1}$ 是 $H^1_0$ 的对偶空间, 定义域为 $[0,1]$. 试证: (1) $\sed{h\sin (2\pi hx);\ h>0}$ 在 $H^{-1}$ 中有界; ( ...
- A fine property of the non-empty countable dense-in-self set in the real line
A fine property of the non-empty countable dense-in-self set in the real line Zujin Zhang School o ...
- Gronwall型不等式
Problem. Suppose $x(t)\in C[0,T]$, and satisfies $$\bex t\in [0,T]\ra 1\leq x(t)\leq C_1+C_2\int_0^t ...
- 调用腾讯、百度翻译API,实现游戏机翻通用程序
最近玩了款steam独立游戏,没中文,只能自己汉化了,用腾讯跟百度的API实现了一个通用的机翻程序(只需要导入JSON文本), 同样,比较懒,还没写,先占坑