首页 > 解决方案 > Python中列表的比较算法

问题描述

我有一个列表,想比较列表中的所有值并存储在空列表中。我有以下列表:

store_list = [0,1,4,5,10,6,7,8]
final_list = [] 
If value of particular index is greater than all the previous indices values and less than all 
the occurring values then that value should store in the empty list.

也许这是一个非常简单的问题,但我无法建立上述逻辑。

所需的输出将是:

final_list = [0, 1, 5]
//0 is less than all the occurring values, so it is in the list
//5 is greater than previous values and less than all the other occurring 
  values so it is in lists 

标签: pythonlist

解决方案


IIUC,这是您要寻找的逻辑吗?如果您想要索引或值的条件,我很困惑。如果要将值与索引进行比较,则可以尝试以下代码。

for index,item in enumerate(store_list):
    if (store_list[index] > index-1) and (store_list[index] < index+1):
        final_list.append(store_list[index])

也许您可以提供所需的输出。这很容易理解。


推荐阅读