字典键和deque,不工作第二个popleft或第一个流行音乐。蟒蛇

时间:2018-04-20 14:54:16

标签: python deque

我是python的初学者,我有一个字典,其密钥名称和值作为deque,当我在我的deque中添加它有效时,但是当我尝试用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 Traceback(最近一次调用最后一次):   文件“main.py”,第23行,in     打印EmployeePoint.dic [“Rafael”]。popleft()。point AttributeError:'list'对象没有属性'point'

1 个答案:

答案 0 :(得分:0)

初始化 deque 可以使用deque([people1]),但您不应该在其中附加一个包含一个项目的列表(例如[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