【问题标题】:Python deque append issuePython双端队列追加问题
【发布时间】:2021-12-13 12:51:39
【问题描述】:

我正在尝试将 2 个项目插入双端队列,每个项目将有 2 个点。完全双端队列应该包含 4 个点。不过好像是8分。 有人,请帮助我避免将这些重复点存储在项目队列中。 下面是代码。

from collections import deque

class Data:
    Points = list()
    
class Point:
    Tag = ""
    
queue = deque()

item1 = Data()
item2 = Data()

point1 = Point()
point2 = Point()
point3 = Point()
point4 = Point()

point1.Tag = "point1"
point2.Tag = "point2"
point3.Tag = "point3"
point4.Tag = "point4"

item1.Points.append(point1)
item1.Points.append(point2)
item2.Points.append(point3)
item2.Points.append(point4)

queue.append(item1)
queue.append(item2)

for it in queue:
    for p in it.Points:
        print(p.Tag)

【问题讨论】:

    标签: python python-3.x append deque


    【解决方案1】:
    class Data:
        Points = list()
    

    这里,Points 是一个在实例之间共享的类属性!但你希望它是一个实例属性

    class Data:
        def __init__(self):
            self.Points = list()
            # self.Points = []
    

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 2011-01-11
      • 1970-01-01
      • 2018-07-02
      • 2023-04-02
      • 2018-09-08
      • 2015-04-26
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多