【问题标题】:check if element exists in priorityQueue of tuples检查元组的priorityQueue中是否存在元素
【发布时间】:2018-02-23 16:30:32
【问题描述】:

假设我有这个代码:

q = PriorityQueue()
a = ((1,1), 10, 0)
b = ((2,2), 99, 200)
q.push(a, 1)
q.push(b, 2)

我想检查元素 (1,1) 是否存在于队列中的任何元组中。有没有办法做到这一点?

【问题讨论】:

  • 对于PriorityQueue,它没有“push”方法,我认为在您的代码中,您的意思是“put”,对吗?

标签: python queue priority-queue


【解决方案1】:

PriorityQueue 对象将其项目存储在可通过queue attribute 访问的列表中。你可以这样做:

>>> q = PriorityQueue()
>>> a = ((1, 1), 10, 0)
>>> q.put(a)
>>> any((1, 1) in item for item in q.queue)
True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多