gpt4 book ai didi

python - 查找数组列表的最小值和最大值

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:27 24 4
gpt4 key购买 nike

假设我在一个数组中有 2 个数组,例如:

main_array = [[1, 2, 3, 4], [4, 5, 6, 7]]

我想找到每个数组的 minmax 并将它们存储在一个数组中。对于以上内容,它将是:

result = [1, 4, 4, 7]

在这种情况下,如何使用 Python 的内置 min()max()

我尝试了 min(main_array)max(main_array) 但这给了我:

result = [1,7]

最佳答案

您可以只在单个列表上使用 min()max() 来获取它的最小值/最大值。您还可以使用列表理解来循环遍历列表中的列表和您要使用的函数:

main_array = [[1,2,3,4], [4,5,6,7]]    
res = [func(l) for l in main_array for func in (min, max)]
print(res)

关于python - 查找数组列表的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31341648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com