当我尝试使用来自集合和双端的rotate()来移动列表中的项目时,我继续收到错误。我使用了集合和双端队列,以便将每个元素移动1或n。
from collections import deque
array= deque["a","b","c","d","e"]
array.rotate(1)
print(array)
执行时我收到以下错误
array= deque["a","b","c","d","e"]
TypeError: 'type' object is not subscriptable
答案 0 :(得分:1)
deque
是一个类的名称:
>>> deque
<class 'collections.deque'>
因此,deque["a","b","c","d","e"]
在语法上不正确。您可以通过实例化它来创建新的deque
对象:deque(["a","b","c","d","e"])
此对象有一个rotate
方法,您可以调用:
>>> array.rotate(1)
>>> print(array)
deque(['e', 'a', 'b', 'c', 'd'])
如果您需要列表对象,可以执行以下操作:list(array)