繁体   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