Python, counts for comparison and list compilation

I have a list containing many subscriptions. i.e.

mylst = [[1, 343, 407, 433, 27], 
         [1, 344, 413, 744, 302], 
         [1, 344, 500, 600, 100], 
         [1, 344, 752, 1114, 363], 
         [1, 345, 755, 922, 168], 
         [2, 345, 188, 1093, 906], 
         [2, 346, 4, 950, 947], 
         [2, 346, 953, 995, 43], 
         [3, 346, 967, 1084, 118], 
         [3, 347, 4, 951, 948], 
         [3, 347, 1053, 1086, 34], 
         [3, 349, 1049, 1125, 77], 
         [3, 349, 1004, 1124, 120], 
         [3, 350, 185, 986, 802], 
         [3, 352, 1018, 1055, 38]]

I want to first classify this list and make another list using three steps. First of all, I want to compare the sublist when the first element in each sublist is the same, i.e. Mylist [a] [0] == 1. Secondly, comparing the second element in the sublist and if the difference between the second element in the sublist and the other second element in the following sulbists is up to 2, then calculate the difference between the third elements or fourth elements. If any difference for the third and fourth elements is less than 10, I want to add a subscription index.

The result I want should be ... as follows: [0, 1, 3, 4, 6, 7, 10, 11, 12]

The following are my naive attempts to do this.

The following are my naive attempts to do this.

def seg(mylist) :
    Segments = []
    for a in range(len(mylist)-1) :
        for index, value in enumerate (mylist) :
            if mylist[a][0] == 1 :
                if abs(mylist[a][1] - mylist[a+1][1]) <= 2 :
                    if (abs(mylist[a][2] - mylist[a+1][2]) <= 10 or 
                        abs(mylist[a][3] - mylist[a+1][3]) <= 10) :
                        Segments.append(index)
return Segments

or

def seg(mylist) :
    Segments= []
    for index, value in enumerate(mylist) :
        for a in range(len(mylist)-1) :
            if mylist[a][0] == 1 :
                try :
                    if abs(mylist[a][1]-mylist[a+1][1]) <= 2 :
                        if (abs(mylist[a][2]-mylist[a+1][2]) <= 10 or
                            abs(mylist[a][3] - mylist[a+1][3]) <= 10) :
                            Segments.append(index)
                except IndexError :
                    if abs(mylist[a][1]-mylist[a+1][1]) <= 2 :
                        if (abs(mylist[a][2]-mylist[a+1][2]) <= 10 or
                            abs(mylist[a][3] - mylist[a+1][3]) <= 10):
                            Segments.append(index)
return Segments

, , . , try , ( ), while, 'for'.

, , ? , "" ? , .

+2
2

, :

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

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

10 <= 2 not < 2 .

OrderedDict :

from collections import OrderedDict

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

, . , Pythonic , , , .

def seg(mylist):
    # converted list to set in case there are any duplicates
    segments = set()

    for entry_index in range(len(mylist)):
        for c in range(len(mylist)):
            first = mylist[entry_index]
            comparison = mylist[c]

            # ignore comparing the same items
            if entry_index == c:
               continue

            # ignore cases where the first item does not match
            if first[0] != comparison[0]:
                continue

            # ignore cases where the second item differs by more than 2
            if abs(first[1] - comparison[1]) > 2:
                continue

            # add cases where the third and fourth items differ by less than 10
            if abs(first[2] - comparison[2]) < 10 or abs(first[3] - comparison[3]) < 10:
                segments.add(entry_index)

            elif abs(first[2] - comparison[3]) < 10 or abs(first[3] - comparison[2]) < 10:
                segments.add(entry_index)

    return segments
0

Source: https://habr.com/ru/post/1660624/


All Articles