【发布时间】:2017-09-26 12:04:15
【问题描述】:
我想在python中为队列列表(自我实现)实现一个检查成员方法,而不需要遍历所有成员来检查并且不牺牲顺序。
我有以下 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) 的检查成员方法?
【问题讨论】:
-
你不能;这就是
dicts 存在的原因。
标签: python list data-structures queue