python - 根据子列表中特定元素的值从列表列表中删除子列表
问题描述
我有一个列表列表,其中包含来自拍卖网站的卖出和出价输入作为子列表。我需要比较 Sell 子列表列表的结束时间戳,如果任何投标子列表的投标时间戳大于结束时间戳,我需要忽略或删除该提交。
到目前为止,我有关闭子列表时间戳,我可以进行比较以找到具有更高出价时间戳的列表,但我无法删除它。当我使用.pop
时,它仍然没有删除。如果它的时间戳大于关闭时间戳,你能告诉我如何删除整个列表吗?
在下面的输入中,第一个列表中的最后一个值“20”是关闭时间。第 4 个列表的起始时间戳为 21,因此我需要将其删除。
我的列表:
['15', '8', 'SELL', 'tv_1', '250.00', '20']
['18', '1', 'BID', 'tv_1', '150.00']
['19', '3', 'BID', 'tv_1', '200.00']
['21', '3', 'BID', 'tv_1', '300.00']
我的代码
for each_entry in each_bid_item:
initial_time = (each_entry[0][0])
close_time = (each_entry[0][5])
reserve_price = (each_entry[0][4])
display_item = (each_entry[0][3])
for entry in each_entry:
if entry[0] > close_time:
entry.pop
解决方案
您可以在此处利用过滤器,或使用列表推导。尝试这样的事情:
lst = [['15', '8', 'SELL', 'tv_1', '250.00', '20'],
['18', '1', 'BID', 'tv_1', '150.00'],
['19', '3', 'BID', 'tv_1', '200.00'],
['21', '3', 'BID', 'tv_1', '300.00']]
# Filter:
lst = list(filter(lambda l: int(l[0]) <= 20, lst))
# List comprehension:
lst = [entry for entry in lst if int(entry[0]) <= 20]
推荐阅读
- angular - this.contacts.push(contact) 有问题;& 如果(数据.n==1)
- linux - 使用 ad-hoc ansible 命令读取环境变量
- javascript - 这个 reactJS 代码中 Tab.TabPanel 的目的是什么?
- bash - 根据 2 个文件中的 1、2、3 列匹配合并 2 个文件并从 2 个文件中打印特定列。还在输出中保留两个文件中的非配对行
- python - Python将每个查询表字段存储在不同的变量中
- sql - Redshift - 如何使用一个表中的列作为 SIMILAR TO 中的模式
- javascript - 输入错误:无法在搜索输入中输入“S”
- html - 最初将宽度设置为适合内容时,CSS宽度过渡不起作用
- vue.js - 如何在 VueJS 中连接/修改道具
- junit - 与 SPOCK 并行运行 ST