首页 > 解决方案 > 如何将列表的值与同一列表中的其他值进行比较?

问题描述

我正在处理我的代码。我想通过比较列表中包含的每个值来知道哪个值大于列表。以下是我创建的列表的示例。

list = ['1192:09:05', 1, -60, 5, -79, 4, -24, ..., n, n+1]

基于该列表,我想知道两者之间最伟大的一个list[2] list[4] list[6] list[n+1]

标签: pythonlist

解决方案


如果我理解了,您想知道列表的最大值。

你可以试试这个:

list = ['1192:09:05', 1, -60, 5, -79, 4, -24, ..., n, n+1]

不得考虑此列表的第一个元素。

max = list[1]
for x in range(1,len(list)):
    if list[x] > max:
        max = list[x]

如果你想知道 list[2] list[4] list[6] list[n+1] 之间的最大数,你可以:

max = list[1]
for x in range(1,len(list),2):
    if list[x] > max:
        max = list[x]

我不知道'n'是什么,我帮不了你。


推荐阅读