Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个元组的最大值_Python_Max - Fatal编程技术网

Python 两个元组的最大值

Python 两个元组的最大值,python,max,Python,Max,Python文档指出,当使用多个参数调用时,max()返回最大的参数 >>> a = (1, 1, 1, 9) >>> b = (4, 5, 6) >>> max(a, b) (4, 5, 6) 在这种情况下,是什么定义了元组的大小?元组a有更多的元素(四个对三个),并且它的最大值(9)大于在b(6)中可以找到的最大值,因此根据任何标准,我都希望它是返回的。max()如何比较元组?它们一次比较一个元素,就像其他序列一样。如果将其与字符串比较

Python文档指出,当使用多个参数调用时,max()返回最大的参数

>>> a = (1, 1, 1, 9)
>>> b = (4, 5, 6)
>>> max(a, b)
(4, 5, 6)

在这种情况下,是什么定义了元组的大小?元组a有更多的元素(四个对三个),并且它的最大值(9)大于在b(6)中可以找到的最大值,因此根据任何标准,我都希望它是返回的。max()如何比较元组?

它们一次比较一个元素,就像其他序列一样。如果将其与字符串比较,则(可能)最容易理解:

>>> (1, 2, 3) > (1, 2, 4)
False
>>> (2, 2, 3) > (1, 2, 4)
True
>>> 'abc' > 'abd'
False
>>> 'bbc' > 'abd'
True

元组和所有其他序列一样是按字典顺序排列的:两个元组的顺序由元组不同的第一个位置决定。引述自:

元组和列表是使用 相应的元素

您的两个元组在第一个位置不同,因为4>1,所以

>>> (4, 5, 6) > (1, 1, 1, 9)
True

它从左到右比较元组的每个元素,直到找到一个大于另一个。然后返回这个元组。比如说

>>> a = (2,0,0,0)
>>> b= (1,1,1,1)
>>> max(a,b)
(2, 0, 0, 0)

>>> b = (2,1,1,1)
>>> max(a,b)
(2, 1, 1, 1)

在一个元组中发现大于另一个元组中相应元素的元素后,剩余值对返回的元组没有影响。

使用<进行比较大致相当于:

def compare(a,b):
    print ""
    print "testing %s < %s" %(str(a),str(b))
    for ai,bi in zip(a,b):
        print "comparing elements",ai,bi
        if ai < bi:
            return True
        if bi < ai:
            return False
    if len(a)<len(b):
        return True
    return False

test_cases = [tuple([1]),(1,2),(1,1),(1,1,1),(None,None,None),tuple([None]),(99,99)]

print "running tests"
for a in test_cases:
    for b in test_cases:
        assert(compare(a,b) == (a<b))
"""
>>>
running tests

testing (1,) < (1,)
comparing elements 1 1

testing (1,) < (1, 2)
comparing elements 1 1

testing (1,) < (1, 1)
comparing elements 1 1

testing (1,) < (1, 1, 1)
comparing elements 1 1

testing (1,) < (None, None, None)
comparing elements 1 None

testing (1,) < (None,)
comparing elements 1 None

testing (1,) < (99, 99)
comparing elements 1 99

testing (1, 2) < (1,)
comparing elements 1 1

testing (1, 2) < (1, 2)
comparing elements 1 1
comparing elements 2 2

testing (1, 2) < (1, 1)
comparing elements 1 1
comparing elements 2 1

testing (1, 2) < (1, 1, 1)
comparing elements 1 1
comparing elements 2 1

testing (1, 2) < (None, None, None)
comparing elements 1 None

testing (1, 2) < (None,)
comparing elements 1 None

testing (1, 2) < (99, 99)
comparing elements 1 99

testing (1, 1) < (1,)
comparing elements 1 1

testing (1, 1) < (1, 2)
comparing elements 1 1
comparing elements 1 2

testing (1, 1) < (1, 1)
comparing elements 1 1
comparing elements 1 1

testing (1, 1) < (1, 1, 1)
comparing elements 1 1
comparing elements 1 1

testing (1, 1) < (None, None, None)
comparing elements 1 None

testing (1, 1) < (None,)
comparing elements 1 None

testing (1, 1) < (99, 99)
comparing elements 1 99

testing (1, 1, 1) < (1,)
comparing elements 1 1

testing (1, 1, 1) < (1, 2)
comparing elements 1 1
comparing elements 1 2

testing (1, 1, 1) < (1, 1)
comparing elements 1 1
comparing elements 1 1

testing (1, 1, 1) < (1, 1, 1)
comparing elements 1 1
comparing elements 1 1
comparing elements 1 1

testing (1, 1, 1) < (None, None, None)
comparing elements 1 None

testing (1, 1, 1) < (None,)
comparing elements 1 None

testing (1, 1, 1) < (99, 99)
comparing elements 1 99

testing (None, None, None) < (1,)
comparing elements None 1

testing (None, None, None) < (1, 2)
comparing elements None 1

testing (None, None, None) < (1, 1)
comparing elements None 1

testing (None, None, None) < (1, 1, 1)
comparing elements None 1

testing (None, None, None) < (None, None, None)
comparing elements None None
comparing elements None None
comparing elements None None

testing (None, None, None) < (None,)
comparing elements None None

testing (None, None, None) < (99, 99)
comparing elements None 99

testing (None,) < (1,)
comparing elements None 1

testing (None,) < (1, 2)
comparing elements None 1

testing (None,) < (1, 1)
comparing elements None 1

testing (None,) < (1, 1, 1)
comparing elements None 1

testing (None,) < (None, None, None)
comparing elements None None

testing (None,) < (None,)
comparing elements None None

testing (None,) < (99, 99)
comparing elements None 99

testing (99, 99) < (1,)
comparing elements 99 1

testing (99, 99) < (1, 2)
comparing elements 99 1

testing (99, 99) < (1, 1)
comparing elements 99 1

testing (99, 99) < (1, 1, 1)
comparing elements 99 1

testing (99, 99) < (None, None, None)
comparing elements 99 None

testing (99, 99) < (None,)
comparing elements 99 None

testing (99, 99) < (99, 99)
comparing elements 99 99
comparing elements 99 99"""
def比较(a,b):
打印“”
打印“测试%s<%s%”(str(a)、str(b))
对于ai,zip中的bi(a,b):
打印“比较元素”,ai,bi
如果ai如果len(a)我也通过反复试验得出了相同的结论,但似乎在我看的任何地方都没有记录,你知道它可能在哪里吗?不,我很惊讶max()的文档是多么的模糊。如果你想要最长的元组,请使用
max(a,b,key=len)
。对于其中数字最大的元组,使用
max(a,b,key=max)
是否在某处记录了类型间比较?我正在寻找一个解释,解释为什么“python”>[130,129]等于True,或者为什么[1,2,3]>999也返回True。不同类型的值的顺序是任意的,因此不应该依赖它。看我给你的推荐信。还有一句话:“否则,不同类型的对象总是比较不相等,并且顺序一致但任意。”另请参见。