循环移位表python

2024-10-02 00:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在实现我目前为止在创建一个真正的定义程序方面遇到了困难。

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在哪里重新创建原始列表,在本例中是4。但是我的练习题是。。。

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部分来完全复制问题。

溶液样品运行:

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部分的任何建议,将不胜感激!:)


Tags: andthetorightbythatfunctionleft
3条回答

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

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行。你可以这样组合

def shift(listL, k, direction):
    if direction == "right":
        k = len(listL) - k
    right = listL[k::]
    left = listL[:k:]
    return right + left

我想main应该是这样的

def main(listL):
    print(listL)
    print(shift(listL, 4, "left"))
    print(shift(listL, 4, "right"))

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

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 are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

看看这个:

>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

循环移位4:

>>> b = a[4::] + a[:4:]
>>> b
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]

并采用两种函数格式:

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

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

打电话给他们:

print shiftLbyn([1,2,3,4,5,6,7,8], 3)
print shiftRbyn([1,2,3,4,5,6,7,8], 4)

将给出输出:

[4, 5, 6, 7, 8, 1, 2, 3]
[5, 6, 7, 8, 1, 2, 3, 4]

相关问题 更多 >

    热门问题