#encoding=utf8 class MQueue: def __init__(self): self.data = [] def get(self): if self.data.__len__()>0: return self.data.pop() else: return None def put(self, item): if not self.isExist(item): self.data.insert(0, item) def remove(self, obj, attr='name'):#移除某个属性等于obj的所有对象 while -1 != self.indexOf(obj): self.data.pop(self.indexOf(obj)) def size(self): return self.data.__len__() def isEmpty(self): return 0 == self.data.__len__() def indexOf(self, obj, attr='name'):#查询对象indexOf(obj),查询元素indexOf(obj.str,obj.attr) isDict = isinstance(obj, dict) for i in range(self.data.__len__()): if (isDict and self.data[i] == obj) or (not isDict and obj == self.data[i][attr]): return i return -1 def isExist(self, obj): return not -1 == self.indexOf(obj) def getData(self): return self.data if __name__ == '__main__': q = MQueue() q.put({'name': '路人甲'}) q.put({'name': '路人1'}) q.put({'name': '路人2'}) q.put({'name': '路人3'}) q.put({'name': '路人甲'}) print(q.size()) q.remove('路人甲') print(q.size()) print(q.isExist('路人1'))