我有这个代码:
template = [1, 2, 3, 4, 5, 6]
db = {
'a': [1, 3, 4, 6],
'b': [2, 3],
'c': [1, 2, 3],
'd': [5, 2, 1],
'e': [1]
}
class Device:
missing = []
def __init__(self, name, numbers):
self.name = name
self.numbers = numbers
def verify(self):
for i in template:
if i not in self.numbers and i not in self.missing:
self.missing.append(i)
def main():
device = dict()
for name in db:
device[name] = Device(name, db[name])
device[name].verify()
for name in device:
print((name, device[name].missing))
if __name__ == '__main__':
main()
我希望对“ db”字典中的每个名称使用此代码来创建对象,该对象将存储该db中的数字。然后它将从“模板”中查找每个缺失的数字,然后将其存储在“缺失”列表中。
我希望这段代码的输出是这样的:
('a', [2, 5])
('b', [1, 4, 5, 6])
('c', [4, 5, 6])
('d', [3, 4, 5])
('e', [2, 3, 4, 5, 6])
但是可以打印
('a', [2, 5, 1, 4, 6, 3])
('b', [2, 5, 1, 4, 6, 3])
('c', [2, 5, 1, 4, 6, 3])
('d', [2, 5, 1, 4, 6, 3])
('e', [2, 5, 1, 4, 6, 3])
我错过了什么吗?当我从字典中调用对象时,似乎无法区分对象。