首页 > 解决方案 > 如何编写代码来获取python列表中每个列表的最小值?

问题描述

我需要帮助编写代码,以帮助我获得 python 列表中每个列表的最低数量。然后在获得的最低值中,我必须以某种方式找到最低数中的最高数。我不允许调用内置函数minmax,或使用预先编写的模块中的任何其他函数。我该怎么做呢?我已经尝试使用以下代码:

for list in ells:
    sort.list(ells)

标签: pythonpython-3.x

解决方案


由于您不允许使用内置函数,因此您可以使用一个变量来跟踪您在遍历子列表时找到的最低数字,并使用另一个变量来跟踪您找到的最低数字中的最高值到目前为止,您遍历列表列表:

l = [
    [2, 5, 7],
    [1, 3, 8],
    [4, 6, 9]
]
highest_of_lowest = None
for sublist in l:
    lowest = None
    for item in sublist:
        if lowest is None or lowest > item:
            lowest = item
    if highest_of_lowest is None or highest_of_lowest < lowest:
        highest_of_lowest = lowest
print(highest_of_lowest)

这输出:4


推荐阅读