簡體   English   中英

將列表一的值與返回列表一的子集的列表二進行比較的最有效方法

[英]Most efficient way to compare the values of list one with list two returning a subset of list one

我有兩個列表列表

trecords = [[1072, 'chickens', 'are', 'messy'], [1073,'chickens', 'eat', 'grass'],...]
srecords = [[1, 'chickens', 'attack', 'lizards'], [12,'chickens', 'eat', 'grass'],...]

我需要比較每個列表的最終值並返回列表 a 中未包含在列表 b 中的數字位...生成一個值列表,如[1072,...]

以下代碼有效:

droplist = []
for trecord in trecords:
    if trecord[1::] not in [srecord[1::] for srecord in srecords]:
        droplist.append(trecord[0])

如果它更快,我寧願有這樣的東西:

droplist = [trecord[0] for trecord in trecords if trecords[1::] not in srecords[1::]]

但這與每個值都匹配,我不知道為什么。 列表的實際長度是每個 300k 值。 這是比較它們的最快方法嗎? 我還將數據放入字典中,數字為鍵,文本(列表)為值,但這似乎更慢。

在這里,我認為您只需要在列表理解中再增加 1 層嵌套\

droplist = [trecord[0] for trecord in trecords if trecord[1:] not in [srecord[1:] for srecord in srecords]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM