python max of list of arrays

前端 未结 4 1658
礼貌的吻别
礼貌的吻别 2021-01-12 10:50

I have a list of arrays like:

a = [array([6,2]),array([8,3]),array([4,2])]

I tried max(a) which returns the following error:

4条回答
  •  梦毁少年i
    2021-01-12 11:02

    The easiest way is to convert to tuple/lists for the sake of comparison (or implement the comparison yourself):

    >>> max(a, key=tuple)
    array([8, 3])
    

    Note this is the builtin max and not np.max

    EDIT:

    For multi dimensional arrays, use the .tolist method:

    max(a, key=operator.methodcaller('tolist'))
    

提交回复
热议问题