在python 3中,int(50)<'2'会导致一个TypeError,而且它应该这样做。但是,在python2.x中,int(50)<'2'返回True (其他数字格式也是如此,但int同时存在于py2和py3中)。我的问题有几个部分:
int小于str是什么意思?ord / chr吗?
'5'和u'5'在这方面有什么区别吗?发布于 2010-11-24 13:06:33
允许进行这些比较的原因是排序。Python2.x可以对包含混合类型的列表进行排序,包括字符串和整数--整数总是首先出现的。Python3.x不允许这样做,这正是您指出的原因。
Python2.x:
>>> sorted([1, '1'])
[1, '1']
>>> sorted([1, '1', 2, '2'])
[1, 2, '1', '2']Python3.x:
>>> sorted([1, '1'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()https://stackoverflow.com/questions/4266918
复制相似问题