Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 轮换名单不起作用?_Python - Fatal编程技术网

Python 轮换名单不起作用?

Python 轮换名单不起作用?,python,Python,运行以下python代码: class MarblesBoard: def __init__(self, marbles): self.input = list(marbles) print(marbles) def switch(self): self.input[1], self.input[0] = self.input[0], self.input[1] #print self.input def

运行以下python代码:

class MarblesBoard:
    def __init__(self, marbles):
        self.input = list(marbles) 
        print(marbles)
    def switch(self):
        self.input[1], self.input[0] = self.input[0], self.input[1]
        #print self.input
    def rotate(self):
        return self.input[1:] + self.input[:1]
        #print self.input
    def is_solved(self):
        if all(self.input[i] <= self.input[i+1] for i in range(len(self.input)-1)):
            return True
            print "True"
        else:
            print "Not solved!"

board = MarblesBoard((3,6,7,4,1,0,8,2,5))
board.switch()
print board.input
board.rotate()
print board.input

board.is_solved()
class大理石板:
定义初始(自我,大理石):
self.input=列表(大理石)
印刷品(大理石)
def开关(自):
self.input[1],self.input[0]=self.input[0],self.input[1]
#打印自我输入
def旋转(自):
返回self.input[1:][self.input[:1]
#打印自我输入
def已解决(自):

如果all(self.input[i]处于当前状态,则
rotate
函数永远不会将自身保存回
self.input
。只需返回新状态即可

def rotate(self):
    return self.input[1:] + self.input[:1]
应将其更改为(类似于您在
开关中所做的操作)
功能:


然后,您的旋转将被保存。

在当前状态下,您的
旋转
函数不会将自身保存回
自我输入
。您只需返回新状态即可

def rotate(self):
    return self.input[1:] + self.input[:1]
应将其更改为(类似于您在
开关中所做的操作)
功能:


然后,您的轮换将被保存。

您将返回轮换列表,而不是将其重新分配回
self。输入:

def rotate(self):
    self.input = self.input[1:] + self.input[:1]

您将返回已旋转的列表,而不是将其重新分配回
self。输入

def rotate(self):
    self.input = self.input[1:] + self.input[:1]

正如其他人指出的,你的代码失败了,因为你丢弃了你在<代码> Reals方法中创建的新列表。但是,你可以考虑使用<代码>集合。

>>> from collections import deque
>>> d = deque(range(10))
>>> d
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> d.rotate(-1)
>>> d
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

注释,这是有效的。这是在<代码> DEQue/COD>中更有效地实现的,因为它是一个双链表,而<代码>列表实际上是一个数组列表。

正如其他人指出的,你的代码失败了,因为你丢弃了在你的代码中创建的新列表>旋转< /C> >方法。但是,你可能会考虑U。使用

collections.deque
代替列表:

>>> from collections import deque
>>> d = deque(range(10))
>>> d
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> d.rotate(-1)
>>> d
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

请注意,这在适当的地方起作用。这在
deque
中更有效地实现,因为它是一个双链接列表,而
list
实际上是一个数组列表。

如果需要更改类对象(输入)本身,请按照上面的建议进行更改

如果您不需要更新它,因为一旦您更新了它,那么所有其他的函数也会被更改,您可能不喜欢

这样做吧-

rotated_board = board.rotate()
print(rotated_board)

如果有效,请告诉我!

如果需要更改类对象(输入)本身,请按照上面的建议进行更改

如果您不需要更新它,因为一旦您更新了它,那么所有其他的函数也会被更改,您可能不喜欢

这样做吧-

rotated_board = board.rotate()
print(rotated_board)

如果成功了,请告诉我!

您希望得到的答案是什么?您是想撤销列表吗?您希望得到的答案是什么?您是想撤销列表吗?非常清楚的解释,谢谢!非常清楚的解释,谢谢!