首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环移位列表python

循环移位列表python
EN

Stack Overflow用户
提问于 2013-10-15 11:24:08
回答 3查看 20.6K关注 0票数 1

我在创建一个真正的清晰度程序时遇到了麻烦。

代码语言:javascript
复制
def left():
    listL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    k=4
    right = listL[k::]
    left = listL[:k:]
    print(right + left)


def right():
    listL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    k=len(listL)-4
    right = listL[k::]
    left = listL[:k:]
    print(right + left)

我的代码根据向左或向右移动k来计算在哪里重新创建原始listL,在本例中是4。然而,我的练习问题询问...

代码语言:javascript
复制
Given a list of N numbers, write a function to shift the numbers circularly by some integer k (where k < N). The function should take the list and k as arguments and return the shifted list. 
a) Write a function that assumes the shifting is to the left. It should not print anything. 
b) Write a function that takes a third argument that specifies shifting left or right. It should not print anything. Perform whatever error-checking you consider necessary. 
c) Write a main() function that calls the above functions. It should print the lists both before and after shifting. Perform whatever error-checking you consider necessary. 

我已经满足了A部分的要求,但我不知道如何构建B部分和C部分来完整地复制问题。

解决方案示例运行:

代码语言:javascript
复制
Sample run 
>>> ================================ RESTART ================================
>>> 
original list:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shifted by 4, to the left: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
shifted by 4, to the right: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]

如果有任何关于我应该如何解决b和c部分的建议,我将不胜感激!:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-15 11:31:18

我认为这不适用于OP,因为这听起来像是CS课程作业,但对于其他正在寻找解决方案的人来说,只需使用:

代码语言:javascript
复制
from collections import deque

d = deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
d.rotate(3)  # to the right
d.rotate(-3)  # back to the left

*编辑

跟进你的评论(来自deque docs):

Deques是堆栈和队列的泛化(该名称发音为“”,是“双端队列”的缩写)。Deques支持线程安全、内存高效的附加和弹出,并且在任意方向上都具有大致相同的O(1)性能。

尽管列表对象支持类似的操作,但它们针对快速的定长操作进行了优化,并为pop(0)和insert(0,v)操作带来O(n)内存移动成本,这些操作同时更改了底层数据表示的大小和位置。

票数 16
EN

Stack Overflow用户

发布于 2013-10-15 11:35:34

看看这个:

代码语言:javascript
复制
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

循环移位4:

代码语言:javascript
复制
>>> b = a[4::] + a[:4:]
>>> b
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]

并采用两个函数的格式:

代码语言:javascript
复制
def shiftLbyn(arr, n=0):
    return arr[n::] + arr[:n:]

def shiftRbyn(arr, n=0):
    return arr[n:len(arr):] + arr[0:n:]

给他们打电话:

代码语言:javascript
复制
print shiftLbyn([1,2,3,4,5,6,7,8], 3)
print shiftRbyn([1,2,3,4,5,6,7,8], 4)

将给出输出:

代码语言:javascript
复制
[4, 5, 6, 7, 8, 1, 2, 3]
[5, 6, 7, 8, 1, 2, 3, 4]
票数 4
EN

Stack Overflow用户

发布于 2013-10-15 11:33:03

首先,更改您的函数以获取参数并返回结果。例如

代码语言:javascript
复制
def left(listL, k):
    right = listL[k::]
    left = listL[:k:]
    return right + left # is this the usual meaning of left and right?

# This is how you call the function
print(left([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4))

现在,如果你注意到leftright有相同的最后3行。您可以这样将它们组合在一起

代码语言:javascript
复制
def shift(listL, k, direction):
    if direction == "right":
        k = len(listL) - k
    right = listL[k::]
    left = listL[:k:]
    return right + left

我猜main应该是这样的

代码语言:javascript
复制
def main(listL):
    print(listL)
    print(shift(listL, 4, "left"))
    print(shift(listL, 4, "right"))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19372771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档