Python3移植:TypeError:unorderable类型:dict()<int()

Her*_*ery 2 python porting python-3.x

我有这段代码在python 2.7中正常工作."dist"是一个数字字典,"min_dist"只是一个数字.

for v in vertices:
    if dist[v.node_id] < min_dist:
        min_dist = dist[v.node_id]
        cur_min = v
Run Code Online (Sandbox Code Playgroud)

现在我试图在python 3.2下运行它,它给了我这个错误:

    if dist[v.node_id] < min_dist:
TypeError: unorderable types: dict() < int()
Run Code Online (Sandbox Code Playgroud)

我在python 3.2中的代码出了什么问题?

Len*_*bro 10

您的代码在Python 2中也是"错误的".将字典与整数进行比较是没有意义的.这就像询问颜色是否大于数字一样,它根本没有意义.

Python 2允许比较这样的不同类型,并且总是说字典大于数字,这是任意的.即使空字典大于数字:

>>> import sys
>>> {} > sys.maxint
True
Run Code Online (Sandbox Code Playgroud)

这样的比较是没有意义的,而Python 3正确地引发和错误,实际上说"我不知道你的意思",这更好,避免错误的比较.