2017-09-26 44 views
-1

我想在python中實現隊列列表(自我實現)的檢查成員方法,而不必遍歷所有成員來檢查並且不犧牲順序。如何在恆定時間O(1)中爲隊列列表實現檢查成員方法?

我有以下FIFOqueue實現:

class FIFOqueue(object): 
"""Initialize with a list""" 
def __init__(self, elements): 
    self.list = elements 

def pop(self): 
    """Return the oldest element of the list (last index)""" 
    return self.list.pop() 

def insert(self, element): 
    """Insert a new element at the end""" 
    self.list = self.list + [element] 

如何在恆定時間O(1)添加到上面的類進行檢查成員方法?

+1

你不能;這就是爲什麼'dict's存在。 – chepner

回答