Python 反向字符串 - 5 种方法和最佳方法

Python String 没有内置的 reverse() 函数,但是,在 Python 中有几种方法可以扭转一个字符串。

1、如何在Python中翻译一个字符串?

一些常见的方式来扭转一个字符串是:

使用 Slicing创建字符串的反复副本 *使用 for loop和反向顺序的附加字符 *使用 while loop以反向顺序迭代字符串并附加它们 *使用 string join()函数与 reversed() iterator *创建

1.1)使用Slicing的Python逆行字符串

1def reverse_slicing(s):
2    return s[::-1]
3
4input_str = 'ABç∂EF'
5
6if __name__ == "__main__":
7    print('Reverse String using slicing =', reverse_slicing(input_str))

如果您在 Python 脚本上运行,输出将是:

1Reverse String using slicing = FE∂çBA

1.2) 使用 For Loop 逆行字符串

 1def reverse_for_loop(s):
 2    s1 = ''
 3    for c in s:
 4        s1 = c + s1  # appending chars in reverse order
 5    return s1
 6
 7input_str = 'ABç∂EF'
 8
 9if __name__ == "__main__":
10    print('Reverse String using for loop =', reverse_for_loop(input_str))

输出: 逆行字符串使用为循环 = FEçBA

1.3)使用 While Loop 扭转一个字符串

 1def reverse_while_loop(s):
 2    s1 = ''
 3    length = len(s) - 1
 4    while length >= 0:
 5        s1 = s1 + s[length]
 6        length = length - 1
 7    return s1
 8
 9input_str = 'ABç∂EF'
10
11if __name__ == "__main__":
12    print('Reverse String using while loop =', reverse_while_loop(input_str))

1.4)使用 join() 和 reversed() 扭转一个字符串

1def reverse_join_reversed_iter(s):
2    s1 = ''.join(reversed(s))
3    return s1

1.5) Python 使用 List reverse() 逆行字符串

1def reverse_list(s):
2    temp_list = list(s)
3    temp_list.reverse()
4    return ''.join(temp_list)

1.6) Python 使用回归字符串

1def reverse_recursion(s):
2    if len(s) == 0:
3        return s
4    else:
5        return reverse_recursion(s[1:]) + s[0]

在Python中翻译一个字符串的最佳方法

我们可以通过多种算法扭转一个字符串,我们已经看到六个字符串,但是你应该选择哪个字符串扭转一个字符串,我们可以使用 timeit module来运行这些函数的多个迭代,并获得运行这些函数所需的平均时间。上面的所有函数都存储在名为 `string_reverse.py 的 Python 脚本中。

 1$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*10)'
 2100000 loops, best of 5: 0.449 usec per loop
 3
 4$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_list("ABç∂EF"*10)'
 5100000 loops, best of 5: 2.46 usec per loop
 6
 7$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)'
 8100000 loops, best of 5: 2.49 usec per loop
 9
10$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_for_loop("ABç∂EF"*10)'
11100000 loops, best of 5: 5.5 usec per loop
12
13$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_while_loop("ABç∂EF"*10)'
14100000 loops, best of 5: 9.4 usec per loop
15
16$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*10)'
17100000 loops, best of 5: 24.3 usec per loop

Python Reverse String The below table presents the results and slowness of an algorithm from the best one. <!--

──」

AlgorithmTimeIt Execution Time (Best of 5)Slowness
Slicing0.449 usec1x
List reverse()2.46 usec5.48x
reversed() + join()2.49 usec5.55x
for loop5.5 usec12.25x
while loop9.4 usec20.94x
Recursion24.3 usec54.12x

3、总结

我们应该使用切割来扭转一个字符串在Python中。它的代码非常简单和小,我们不需要写自己的逻辑来扭转字符串。

您可以从我们的 GitHub 存储库中查阅完整的 Python 脚本和更多 Python 示例。

四、参考

Published At
Categories with 技术
Tagged with
comments powered by Disqus