python字符串反向输出_Python反向字符串–5种方法和最佳方法

更新时间:2023-06-07 18:01:51 阅读: 评论:0

python字符串反向输出_Python反向字符串–5种⽅法和最佳⽅
python字符串反向输出
Python String doesn’t have a built-in rever() function. However, there are various ways to rever a string in Python. Python String没有内置的rever()函数。 但是,有多种⽅法可以在Python中反转字符串。
1.如何在Python中反转字符串? (1. How to Rever a String in Python?)
Some of the common ways to rever a string are:
反转字符串的⼀些常见⽅法是:
Using to create a rever copy of the string.
使⽤来创建字符串的反向副本。
Using and appending characters in rever order
使⽤并以相反顺序附加字符
Using to iterate string characters in rever order and append them
使⽤以相反的顺序迭代字符串字符并追加它们
Using function with iterator
在迭代器中使⽤函数
Creating a from the string and then calling its rever() function
从字符串创建 ,然后调⽤其rever()函数
Using Recursion
使⽤递归
1.1)使⽤切⽚的Python反向字符串 (1.1) Python Rever String using Slicing)
def rever_slicing(s):
return s[::-1]
input_str = 'ABç∂EF'
if __name__ == "__main__":
print('Rever String using slicing =', rever_slicing(input_str))
If you run above Python script, the output will be:
如果您在Python脚本上运⾏,则输出将是:
Rever String using slicing = FE∂çBA
1.2)使⽤For循环反向字符串 (1.2) Rever String using For Loop)
def rever_for_loop(s):
肺在志为s1 = ''
for c in s:
血栓心脉宁胶囊s1 = c + s1  # appending chars in rever order
return s1
input_str = 'ABç∂EF'
if __name__ == "__main__":
分数乘法练习题
print('Rever String using for loop =', rever_for_loop(input_str))
Output: Rever String using for loop = FE∂çBA
输出: Rever String using for loop = FE∂çBA
1.3)使⽤While循环反转字符串 (1.3) Rever a String using While Loop)
def rever_while_loop(s):
s1 = ''
length = len(s) - 1
睡眠不足的危害有哪些
while length >= 0:
s1 = s1 + s[length]
length = length - 1
return s1
input_str = 'ABç∂EF'
if __name__ == "__main__":
print('Rever String using while loop =', rever_while_loop(input_str))
1.4)使⽤join()和reverd()反转字符串 (1.4) Rever a String using join() and reverd())竞聘优势
鲁鲁修头像
def rever_join_reverd_iter(s):
s1 = ''.join(reverd(s))
return s1
1.5)使⽤列表rever()的Python反向字符串 (1.5) Python Rever String using List rever())
def rever_list(s):
temp_list = list(s)
ver()
return ''.join(temp_list)
1.6)使⽤递归的Python反向字符串 (1.6) Python Rever String using Recursion)
def rever_recursion(s):
if len(s) == 0:
return s
el:
return rever_recursion(s[1:]) + s[0]
2.⽤Python反转字符串的最佳⽅法 (2. Best Way to Rever a String in Python)
We can rever a string through multiple algorithms. We have already en six of them. But which of them you should choo to rever a string.
我们可以通过多种算法反转字符串。 我们已经看过其中的六个。 但是您应该选择其中的哪⼀个反向字符串。
We can u to run multiple iterations of the functions and get the average time required to run them.
我们可以使⽤来运⾏这些函数的多次迭代,并获得运⾏它们所需的平均时间。
All the above functions are stored in a python script named string_rever.py. I executed all the functions one by one for 1,00,000 times using the and got the average of the best 5 runs.
以上所有功能均存储在名为string_rever.py的python脚本中。 我使⽤⼀个接⼀个地执⾏了所有这些功能1,00,000次,并得到了最佳5次运⾏的平均值。
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_slicing("ABç∂EF"*10)'
100000 loops, best of 5: 0.449 uc per loop
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_list("ABç∂EF"*10)'
100000 loops, best of 5: 2.46 uc per loop
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_join_reverd_iter("ABç∂EF"*10)'
100000 loops, best of 5: 2.49 uc per loop
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_for_loop("ABç∂EF"*10)'
100000 loops, best of 5: 5.5 uc per loop
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_while_loop("ABç∂EF"*10)'
100000 loops, best of 5: 9.4 uc per loop
$ python3.7 -m timeit --number 100000 --unit uc 'import string_rever' 'ver_recursion("ABç∂EF"*10)'
100000 loops, best of 5: 24.3 uc per loop
The below table prents the results and slowness of an algorithm from the best one.
下表列出了最佳算法的结果和慢度。
Algorithm TimeIt Execution Time (Best of 5)Slowness
Slicing0.449 uc1x
List rever()  2.46 uc  5.48x
reverd() + join()  2.49 uc  5.55x
for loop  5.5 uc12.25x
while loop9.4 uc20.94x
Recursion24.3 uc54.12x
算法TimeIt执⾏时间(最佳5)缓慢
切⽚0.449微秒1倍
列出rever()  2.46微秒  5.48倍
适合孕妇喝的牛奶reverd()+ join()  2.49微秒  5.55倍
for循环  5.5微秒12.25倍
while循环9.4⽤20.94倍
递归24.3微秒54.12倍
工作证明3.总结 (3. Summary)
We should u slicing to rever a string in Python. Its code is very simple and small and we don’t need to write our own logic to rever the string. Also, it’s the fastest way to rever a string as identified by the above test executions.
我们应该使⽤切⽚来反转Python中的字符串。 它的代码⾮常简单⼩巧,我们不需要编写⾃⼰的逻辑来
反转字符串。 另外,这是反转上述测试执⾏所确定的字符串的最快⽅法。
. 检出完整的python脚本和更多Python⽰例。
4.参考 (4. References)
python字符串反向输出

本文发布于:2023-06-07 18:01:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/895865.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   反向   反转   编写   迭代   算法   字符   顺序
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图