Program 1:
(1, "zebra") < (2, "apple") // true because 1 is less than 2, "zebra" and "apple" are not compared
Program 2:
(3, "apple") < (3, "bird") // true because 3 is equal to 3, "apple" is less than "bird"
Program 3:
(4, "dog") == (4, "dog") // true because 4 is equal to 4, "dog" is equal to "dog"
这些操作符在元组之间有什么用处
为什么苹果比鸟还少?
2 回答
因为苹果的A在罗马字母表中出现在B之前 . 字母顺序是ABCDE ...
元组一次被比较一个元素,直到所有元素都被比较或者你得到答案 .
这是真的,因为
1 < 2
. 没有必要更进一步,因为从左到右检查,一旦发现<
对于一个元素是true
,整个元组就是true
. 这与两个整数具有相同位数的整数完全相同;19 < 23
因为十个's digit. There is no need to look at the one'的数字 .字符串按字母顺序进行比较 .
"apple" < "zebra"
因为它首先按字母顺序排列(就像你按字母顺序排列单词列表一样) .这是真的,因为
1 < 2
. 我们甚至在考虑"zebra"
和"apple"
之前停了下来,因为它们在这里没关系,比较已经满足<
.在这种情况下,我们比较
3
和3
,但它们're equal. At this point, we don't有足够的信息来决定第一个元组是否是<
第二个元组,所以我们继续下一个项目 . 自"apple" < "bird"
以来,我们得出结论,整个第一元组小于第二元组 . 这就像比较30
和31
一样 . 你看十个's digit and see that they are the same, so you move on to comparing the one'的数字 .比较两个元组的相等性时,可以从左到右比较元素 . 一旦发现不匹配就可以退出并得出结论答案是
false
. 要确定答案是true
,您必须比较所有相应的元素并发现它们都匹配 .