如何从多个列表中找到最大值? [英] How to find max values from multiple lists?

查看:164
本文介绍了如何从多个列表中找到最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个相同大小的列表(或numpy数组),我想返回一个相同大小的数组,并在每个点取最大值.

I have multiple lists (or numpy arrays) of the same size and I want to return an array of the same size with the max value at each point.

例如,

A = [[0,1,0,0,3,0],[1,0,0,2,0,3]]
B = [[1,0,0,0,0,4],[0,5,6,0,1,1]]
C = numpy.zeros_like(A)
for i in xrange(len(A)):
    for j in xrange(len(A[0])):
        C[i][j] = max(A[i][j],B[i][j])

结果为C = [[1,1,0,0,3,4],[1,5,6,2,1,3]]

这可以正常工作,但效率不是很高-特别是对于我拥有的数组大小和需要比较的数组数量而言.我如何才能更有效地做到这一点?

This works fine, but is not very efficient - especially for the size of arrays that I have and the number of arrays I need to compare. How can I do this more efficiently?

推荐答案

使用 :

numpy.maximum(x1, x2[, out])
数组的按元素最大值 元素.

numpy.maximum(x1, x2[, out])
Element-wise maximum of array elements.

比较两个数组,并返回一个新的包含按元素排列的数组 最大值.如果要比较的元素之一是nan,则表示 元素被返回.如果两个元素都是nans,那么第一个是 回来.后一种区分对于复杂的Nans很重要, 被定义为至少一个实部或虚部是 南最终的结果是,NAN得以传播.

Compare two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a nan, then that element is returned. If both elements are nans then the first is returned. The latter distinction is important for complex nans, which are defined as at least one of the real or imaginary parts being a nan. The net effect is that nans are propagated.

这篇关于如何从多个列表中找到最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆