python移动列表元素
之前我遇到一个问题,如下
输入:
['hello', 'world', 'hello', 'python']
2
输出:
['hello', 'python', 'hello', 'world']
或者是:
输入:
[1, 2, 3, 4, 5, 6]
1
输出:
[6, 1, 2, 3, 4, 5]
想了半天也没想明白,最后发现collection.deque有个rorate方法:
def moveitems(l: list, number: int):
result = deque(l)
result.rotate(number)
return list(l)