Python, comparison sublists and making a list

后端 未结 2 543
无人共我
无人共我 2021-01-22 10:57

I have a list that contains a lot of sublists. i.e.

mylst = [[1, 343, 407, 433, 27], 
         [1, 344, 413, 744, 302], 
         [1, 344, 500, 600, 100], 
              


        
2条回答
  •  醉酒成梦
    2021-01-22 11:29

    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
    

    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], 
    

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

    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]
    

提交回复
热议问题