Python_列表中找最大值

时间:2025-02-10 08:16:18

把第一个元素作为最大值,依次与后面的元素对比,比后面的小,则重新赋值最大值,循环完成后得到最大值

"""
    list01=[4,5,65,76,7,8]
    不用max函数,在列表中找出最大值
"""
list01 = [4, 5, 65, 76, 7, 8]
# 把第一个元素作为最大值
max_value = list01[0]
# 从第二个开始对比
for i in range(1, len(list01)):
    if max_value < list01[i]:
        max_value = list01[i]

print(max_value)

运行结果

76