列表的轮换也已在前面讨论过, 但是这篇特定的文章集中于速记和各种短技巧, 以单行或一个单词实现。此操作对于程序员完成各种任务至关重要。
让我们讨论一下轮换列表的不同方法。
方法1:使用切片
这种特定的方法是通用方法, 通常用于完成此任务, 并且在许多文章中也进行了讨论。给定转数, 只需将后面的切片部分与初始切片的部分连接即可。
# Python3 code to demonstrate
# rotation of list
# using slice # initializing list
test_list = [ 1 , 4 , 6 , 7 , 2 ]# printing original list
print ( "Original list : " + str (test_list))# using slicing to left rotate by 3
test_list = test_list[ 3 :] + test_list[: 3 ]# Printing list after left rotate
print ( "List after left rotate by 3 : " + str (test_list))# using slicing to right rotate by 3
# back to Original
test_list = test_list[ - 3 :] + test_list[: - 3 ]# Printing after right rotate
print ( "List after right rotate by 3(back to original) : "
+ str (test_list))
输出如下:
Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3 ( back to original) : [1, 4, 6, 7, 2]
方法2:使用列表理解
这个问题也可以通过幼稚的方法解决, 但是它的较短实现将借助于列表理解。在这种方法中, 我们只需要在旋转后将索引分配给每个值到特定位置。
# Python3 code to demonstrate
# rotation of list
# using list comprehension# initializing list
test_list = [ 1 , 4 , 6 , 7 , 2 ]# printing original list
print ( "Original list : " + str (test_list))# using list comprehension to left rotate by 3
test_list = [test_list[(i + 3 ) % len (test_list)]
for i, x in enumerate (test_list)]# Printing list after left rotate
print ( "List after left rotate by 3 : " + str (test_list))# using list comprehension to right rotate by 3
# back to Original
test_list = [test_list[(i - 3 ) % len (test_list)]
for i, x in enumerate (test_list)]# Printing after right rotate
print ( "List after right rotate by 3(back to original) : "
+ str (test_list))
输出如下:
Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3(back to original) : [1, 4, 6, 7, 2]
方法3:使用collections.deque.rotate()
收集模块具有双端队列类, 该类提供rotate(), 这是允许旋转的内置函数。这是鲜为人知的功能, 但实用性更高。
# Python3 code to demonstrate
# rotation of list
# using rotate()
from collections import deque# initializing list
test_list = [ 1 , 4 , 6 , 7 , 2 ]# printing original list
print ( "Original list : " + str (test_list))# using rotate() to left rotate by 3
test_list = deque(test_list)
test_list.rotate( - 3 )
test_list = list (test_list)# Printing list after left rotate
print ( "List after left rotate by 3 : " + str (test_list))# using rotate() to right rotate by 3
# back to Original
test_list = deque(test_list)
test_list.rotate( 3 )
test_list = list (test_list)# Printing after right rotate
print ( "List after right rotate by 3(back to original) : "
+ str (test_list))
输出如下:
Original list : [1, 4, 6, 7, 2]
List after left rotate by 3 : [7, 2, 1, 4, 6]
List after right rotate by 3(back to original) : [1, 4, 6, 7, 2]
【Python旋转列表的方式】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- 检查一个字符串的最大出现字符是否相同
- 用数组的总大小和该元素的频率之差替换每个元素
- Python继承中的方法解析顺序
- 对于给定数组的任何排列,最大化第一个元素的按位与,并保留其余元素
- 如何使用PHP通过HTML表单在JSON文件中附加数据()
- 将给定矩阵转换为排序的螺旋矩阵
- 查找一个N x N网格,其每行和每列的xor相等
- 算法题(满足给定方程的最小正整数X)
- [转] 在安卓设备上使用 Chrome 远程调试功能