首页 > 解决方案 > 根据子列表中特定元素的值从列表列表中删除子列表

问题描述

我有一个列表列表,其中包含来自拍卖网站的卖出和出价输入作为子列表。我需要比较 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

标签: pythonlist

解决方案


您可以在此处利用过滤器,或使用列表推导。尝试这样的事情:


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]

推荐阅读