且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python,比较子列表和列表

更新时间:2023-11-28 23:47:16

您将不得不抓住重复的机会索引,但效率应该更高:

You will have to catch the duplicate indexes but this should be a lot more efficient:

gr = []
it = iter(mylst)
prev = next(it)

for ind, ele in enumerate(it):
    if ele[0] == prev[0] and abs(ele[1] - prev[1]) <= 2:
        if any(abs(ele[i] - prev[i]) < 10 for i in (2, 3)):
            gr.extend((ind, ind+1))
    prev = ele

根据您的逻辑6和7不应显示,因为它们不符合条件:

Based on your logic 6 and 7 should not appear as they don't meet the criteria:

     [2, 346, 953, 995, 43], 
     [3, 346, 967, 1084, 118], 

也应显示10是< = 2 而不是< 2 根据您的描述。

Also for 10 to appear it should be <= 2 not < 2 as per your description.

您可以使用OrderedDict删除重复项并保留订单:

You could use an OrderedDict to remove the dupes and keep the order:

from collections import OrderedDict

print(OrderedDict.fromkeys(gr).keys())
[0, 1, 3, 4, 10, 11, 12]