python 中嵌套数组列表的总最大值
Total max of a nested list of arrays in python
我想要一个获取多个嵌套数组列表和 returns 只有一个数字的函数:输入中出现的所有数字的全局最大值(或最小值)。
例如:
>>> total_max(3.5,[4.5,1,[2,3], np.random.uniform(size=(5,6)),4],[2,-3])
4.5
我不想在 Finding The Largest Number in a Nested List in Python or python max of list of arrays. And I want it to also work with numpy arrays, which is not the case for Total max of a nested list.
中输出多个数字
import numpy as np
def nested_max(nestedList):
if not (isinstance(nestedList, list)):
return np.max(nestedList)
else:
return max([nested_max(a) for a in nestedList])
def nested_min(nestedList):
if not (isinstance(nestedList, list)):
return np.min(nestedList)
else:
return min([nested_min(a) for a in nestedList])
def total_max(*args):
return max([nested_max(a) for a in args])
def total_min(*args):
return min([nested_min(a) for a in args])
def total_range(*args):
return total_min(*args), total_max(*args)
这样得到例如:
>>> total_range(3.5,[4.5,1,[2,3], np.random.uniform(size=(5,6)),4],[2,-3])
(-3, 4.5)
这可以用于 等应用程序,其中可以简单地写:
mi, ma = total_range(dfz, xx)
而不是
mi = np.min((dfz.min(), xx.min()))
ma = np.max((dfz.max(), xx.max()))
求组合图的总体范围。
有更好的变体吗?例如:支持更多类型的元组、集合、数组和列表?更短的代码?计算效率更高?
我想要一个获取多个嵌套数组列表和 returns 只有一个数字的函数:输入中出现的所有数字的全局最大值(或最小值)。
例如:
>>> total_max(3.5,[4.5,1,[2,3], np.random.uniform(size=(5,6)),4],[2,-3])
4.5
我不想在 Finding The Largest Number in a Nested List in Python or python max of list of arrays. And I want it to also work with numpy arrays, which is not the case for Total max of a nested list.
中输出多个数字import numpy as np
def nested_max(nestedList):
if not (isinstance(nestedList, list)):
return np.max(nestedList)
else:
return max([nested_max(a) for a in nestedList])
def nested_min(nestedList):
if not (isinstance(nestedList, list)):
return np.min(nestedList)
else:
return min([nested_min(a) for a in nestedList])
def total_max(*args):
return max([nested_max(a) for a in args])
def total_min(*args):
return min([nested_min(a) for a in args])
def total_range(*args):
return total_min(*args), total_max(*args)
这样得到例如:
>>> total_range(3.5,[4.5,1,[2,3], np.random.uniform(size=(5,6)),4],[2,-3])
(-3, 4.5)
这可以用于
mi, ma = total_range(dfz, xx)
而不是
mi = np.min((dfz.min(), xx.min()))
ma = np.max((dfz.max(), xx.max()))
求组合图的总体范围。
有更好的变体吗?例如:支持更多类型的元组、集合、数组和列表?更短的代码?计算效率更高?