相关疑难解决方法(0)

在Python中,元组比较是如何工作的?

我一直在阅读Core Python编程书,作者展示了一个例子:

(4, 5) < (3, 5) # Equals false
Run Code Online (Sandbox Code Playgroud)

所以,我想知道,它是如何/为什么它等于假?python如何比较这两个元组?

顺便说一下,书中没有解释.

python comparison tuples

147
推荐指数
2
解决办法
13万
查看次数

TypeError:'State'和'State'PYTHON 3的实例之间不支持'<'

我正在尝试使用队列类中的PriorityQueue.但是,我在将自定义对象放入PQ时遇到问题.我已经实现了__cmp__以下功能:

def __cmp__(self, other):
    return (self.priority > other.priority) - (self.priority < other.priority)
Run Code Online (Sandbox Code Playgroud)

我希望PriorityQueue按优先级字段排序,在init函数中指定:

def __init__(self, board, priority=0):
    self.priority = priority
    # Other logic
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码将一个State对象插入PQ时,我收到此错误: TypeError: '<' not supported between instances of 'State' and 'State'

这是运行PQ的代码.

if op.precond(S):
            new_state = op.state_transf(S)
            if not (OPEN.queue.__contains__(new_state)) and not (new_state in CLOSED):
                GVALUES[Problem.hash(new_state)] = get_distance_value(op, new_state)
                HEUR_VALUES[Problem.hash(new_state)] = get_AStar_value(new_state)
                print("NEW STATE: " + str(new_state))
                OPEN.put(new_state)
                print("OPEN: " + str(OPEN.queue))
Run Code Online (Sandbox Code Playgroud)

其中OPEN是priorityQueue.

任何帮助都将非常感谢...因为将值插入PQ应该非常简单.

python priority-queue python-3.x

1
推荐指数
2
解决办法
3897
查看次数

标签 统计

python ×2

comparison ×1

priority-queue ×1

python-3.x ×1

tuples ×1