Python中倒排列是倒序排列的意思。
Python中倒序排列的方法:
1、数组倒序:
原始元素的倒序排列
arr = [1,2,3,4,3,4]
(1)
print arr[::-1]
---->[4, 3, 4, 3, 2, 1]
(2)
arr.reverse() print arr
---->[4, 3, 4, 3, 2, 1]
(3
reversed(arr) #返回一个倒序可遍历对象,需序遍历出 arr = [1,2,3,4,3,4] reversed_arr = [] for i in reversed(arr): reversed_arr.append(i) print reversed_arr
---->[4, 3, 4, 3, 2, 1]
2、字符串倒序:
(1)利用字符串截取
param = 'hello' print param[::-1]
---->'olleh'
(2)利用reversed()返回倒可迭代对象(字符串实现)
param = 'hello' rev_str = '' for i in reversed(param): rev_str += i print rev_str
---->'olleh'
(3)利用reversed()返回倒可迭代对象(数组实现)
rev_arr = [] for i in reversed(param): rev_arr.append(i) print ''.join(rev_arr)
---->'olleh'
更多Python知识,请关注:!!