[英]Python initialize a list in a class
I am trying to solve a leetcode question using list: Design Circular Queue.我正在尝试使用列表解决 leetcode 问题:设计循环队列。
But it throws an error:但它会引发错误:
IndexError: list assignment index out of range
self.queue[self.tail] = value
My code:我的代码:
class MyCircularQueue:
def __init__(self, k: int):
self.size = k
self.queue = []
self.head = -1
self.tail = -1
def enQueue(self, value: int) -> bool:
if self.tail + 1 == self.head: #queue full
return False
elif self.tail + 1 == self.size:
self.tail = 0
self.queue[self.tail] = value
return True
else:
self.tail += 1
self.queue[self.tail] = value
return True
def deQueue(self) -> bool:
if len(self.queue) == 0: #queue empty
return False
elif self.tail + 1 == self.size:
self.queue.pop(self.head)
self.head = -1
return True
else:
self.queue.pop(self.head)
self.head += 1
return True
def Front(self) -> int:
if self.head == -1:
return False
return self.queue[self.head]
def Rear(self) -> int:
if self.tail == -1:
return False
return self.queue[self.tail]
def isEmpty(self) -> bool:
if len(self.queue) == 0:
return True
return False
def isFull(self) -> bool:
if len(self.queue) == self.size:
return True
return False
Other similar posts say maybe the list is not initialized but I can't figure out what I did wrong here.其他类似的帖子说可能列表未初始化但我无法弄清楚我在这里做错了什么。
Question link: https://leetcode.com/problems/design-circular-queue/问题链接: https://leetcode.com/problems/design-circular-queue/
Edit:编辑:
As pointed out below, append
will be an organic way to do it in Python. However, I need index to implement circular queue so I took a different approach:如下所述, append
将是 Python 中的一种有机方式。但是,我需要索引来实现循环队列,因此我采用了不同的方法:
self.tail = (self.tail + 1) % self.size #example circular increment
pop
deQueue:用pop
以外的 None 替换项目Updated code:更新代码:
class MyCircularQueue:
def __init__(self, k: int):
self.size = k
self.queue = [None] * k #replace queue = [] -> add queue[idx] will throw a error
self.head = -1
self.tail = -1
def enQueue(self, value: int) -> bool:
if self.isFull() == True: #queue full
return False
elif self.tail == -1: #add first item
self.tail += 1
self.head += 1
self.queue[self.tail] = value
else:
self.tail = (self.tail + 1) % self.size
self.queue[self.tail] = value
return True
def deQueue(self) -> bool:
if self.isEmpty() == True: #queue empty
return False
elif self.head == self.tail: #at last item
self.queue[self.head] = None
self.head = -1
self.tail = -1
else:
self.queue[self.head] = None #replace item with None other than pop, which will remove None from the list
self.head = (self.head + 1) % self.size #not self.head += 1
return True
def Front(self) -> int:
if self.head == -1:
return -1
return self.queue[self.head]
def Rear(self) -> int:
if self.tail == -1:
return -1
print(self.tail)
return self.queue[self.tail]
def isEmpty(self) -> bool:
if self.head == -1:
return True
return False
def isFull(self) -> bool:
if (self.tail + 1) % self.size == self.head:
return True
return False
Reference: Circular Queue Structure Explanation参考:循环队列结构解释
queue[index]
refers to the index
th item in the list queue
. queue[index]
指的是列表queue
中的第index
项。
If there are no items in the list, index 0
does not exist, so you can't access queue[0]
.如果列表中没有项目,则索引0
不存在,因此您无法访问queue[0]
。
The exception "list assignment index out of range" raises when index >= len(list)
.当index >= len(list)
时引发异常“列表分配索引超出范围”。 You initialize self.tail
to be 0, and the length of the list is also 0.你将self.tail
初始化为 0,列表的长度也为 0。
The Pythonic way to add an item to the last place of a list is to use list.append
:将项目添加到列表最后位置的 Pythonic 方法是使用list.append
:
self.queue.append(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.