python - 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
解决方案
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])
也许您可以提供所需的输出。这很容易理解。
推荐阅读
- reactjs - 单元测试连接组件时如何修复错误“在上下文连接(App)中找不到存储”?
- c# - 如何停止 PdfReader.ComputeUserPassword() 返回 null?(AES256 加密 PDF)
- batch-file - 如何使用时间戳重命名 PDFtk-server 文件?
- html - 有没有一种方法可以“自由地”在弹性框中放置一个项目,而不管其他项目的位置如何?
- php - 从 SpreadsheetReader 计算总行数和列数
- swift - 如何制作动态泛型?
- service-worker - Apollo 与 Next.js 项目中的服务人员
- javascript - 使用 RequestHook 覆盖浏览器语言
- php - 将数据从 php 更新到 mysql 创建空白数据
- xml - 当它可以出现在层次结构的不同位置时匹配第一个元素?