字典键和双端队列,不工作第二个 popleft 或第一个 pop。 python

Dictionary key and deque, dont work second popleft or first pop. python

我是 python 的初学者,我有一个字典,其中的键名和值作为双端队列,当我添加到我的双端队列时它有效,但是当我尝试使用 popleft 退出时,只有第一个有效

from collections import deque

class EmployeePoint:

    dic = {}

    def __init__(self,name,point):

        self.name = name
        self.point = point


people1 = EmployeePoint("Rafael","18")
people2 = EmployeePoint("Rafael","19")
people3 = EmployeePoint("Rafael","20")

EmployeePoint.dic["Rafael"] = deque([people1])
EmployeePoint.dic["Rafael"].append([people2])
EmployeePoint.dic["Rafael"].append([people3])


print EmployeePoint.dic["Rafael"].popleft().point
print EmployeePoint.dic["Rafael"].popleft().point

回溯:

18 追溯(最近一次通话): 文件 "main.py",第 23 行,位于 打印 EmployeePoint.dic["Rafael"].popleft().point AttributeError: 'list' 对象没有属性 'point'

使用 deque([people1]) 初始化 deque 没问题,但您不应该附加一个包含其中一项的列表(即 [people2] 而不是 .append(people2)

from collections import deque

class EmployeePoint:

    dic = {}

    def __init__(self,name,point):

        self.name = name
        self.point = point


people1 = EmployeePoint("Rafael","18")
people2 = EmployeePoint("Rafael","19")
people3 = EmployeePoint("Rafael","20")

EmployeePoint.dic["Rafael"] = deque([people1])
EmployeePoint.dic["Rafael"].append(people2)
EmployeePoint.dic["Rafael"].append(people3)


print (EmployeePoint.dic["Rafael"].popleft().point)
print (EmployeePoint.dic["Rafael"].popleft().point) 
# py3 print paretheses 

输出:

18

19