【问题标题】:Maximum of two tuples最多两个元组
【发布时间】:2012-03-23 11:19:45
【问题描述】:

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

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

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

【问题讨论】:

  • 如果你想要最长的元组,使用max(a, b, key=len)。对于其中数量最多的元组,请使用max(a, b, key=max)

标签: python max


【解决方案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"""

【讨论】:

    【解决方案2】:

    像所有其他序列一样,元组按字典顺序排列:两个元组的顺序由元组不同的第一个位置决定。引用python reference:

    元组和列表使用比较的字典顺序进行比较 对应的元素。

    你的两个元组在第一个位置不同,因为 4 > 1,我们有

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

    【讨论】:

    • 类型间比较是否记录在某处?我正在寻找解释为什么 "python" > [130, 129] 等于 True,或者为什么 [1, 2, 3] > 999 也返回 True。
    • 不同类型的值的顺序是任意的,所以不要依赖它。请参阅我提供的参考。再引用一句:“否则,不同类型的对象总是比较不平等,并且顺序一致但随意。”
    • 另请参阅,this question
    【解决方案3】:

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

    >>> 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)
    

    在一个元组中找到一个大于另一个元组中相应元素的元素后,其余值对返回哪个元组没有影响。

    【讨论】:

    • 经过反复试验,我也得出了相同的结论,但似乎在我查看的任何地方都没有记录,知道它可能在哪里吗?
    • 不,我很惊讶 max() 的文档真的如此模糊。
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-19
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多