Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.
If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).
所以从这个:
>>> a = (100, 0)
>>> b = (50, 50)
>>> a > b
True
但我想按顺序比较2个元组的所有元素,所以在功能上我想要类似的东西(使用上面的值):
>>> a > b
(True, False) #returned tuple containing each comparison
>>> all(a > b)
False
作为一个例子,在实践中,对于像屏幕坐标这样的东西,如果你想检查某些东西是否在(0,0)的屏幕“内部”,但做了一个比较,如coord> (0,0),如果x坐标大于0,但是y坐标更小,它仍然会返回true,这不是这种情况下所需要的.
作为一个子问题/讨论:
我不确定为什么以这种方式返回比较2个不同值的元组.你没有得到任何类型的索引,所以你从比较一个元组(不是测试相等)得到的唯一一点就是在元组的某个点上,其中一个比较会在它们出现时抛出一个真或假的值.不平等.你怎么能利用这个呢?