Home > database >  Python 2D lists: sort 2D list in relation to other sorted 2D list
Python 2D lists: sort 2D list in relation to other sorted 2D list

Time:04-07

I am trying to sort two 2D lists along with two lists, each relative to one of the 2D lists.

The two 2D lists are lists of coordinates captures from 2D and 3D looking something like this:

2D_list1 = [[0.1, 0.7], [0.2, 0.2], [0.5, 0.2], [0.5, 0.6], [0.7, 0.2], [0.8, 0.7]]
2D_list2 = [[0.5, 0.7], [0.5, 0.1], [0.2, 0.7], [0.8, 0.2], [0.8, 0.7], [0.2, 0.2]]

My first approach was to sort the lists and their relative lists by the coordinates with code like this:

2D_list1, relative_list1 = (list(t) for t in zip(*sorted(zip(2D_list1, relative_list1), key=lambda k: [k[0][0], k[0][1]])))

but because the coordinates captured in 2D and 3D are not entirely the same i.e. they can vary by a tenth on both the x and y axis. This will sometimes lead to one list being sorted in another order than the other.

My thought now is that maybe there is a way I can sort the second 2D list and its relative list by how similar the coordinates are to the x,y sorted first list, but I have zero clue about how I would go around doing this.

My expected output from the example values would be:

2D_list1 = [[0.1, 0.7], [0.2, 0.2], [0.5, 0.2], [0.5, 0.6], [0.7, 0.2], [0.8, 0.7]]
2D_list2 = [[0.2, 0.7], [0.2, 0.2], [0.5, 0.1], [0.5, 0.7], [0.8, 0.2], [0.8, 0.7]]

Any help on how to approach this would be greatly appreciated.

Edit: Fixed outcome formatting

Error from relative list sort:

relative_list2.sort(key=lambda x: sorted_list2.index(list2[relative_list2.index(x)]))
ValueError: array([[[37, 40, 40],
    [36, 39, 39],
    [36, 39, 39],
    
    [71, 74, 75],
    [71, 74, 75],
    [70, 73, 74]]], dtype=uint8) is not in list

TypeError

relative_list2.sort(key=lambda x: sorted_list2.index(list2[x]))
TypeError: only integer scalar arrays can be converted to a scalar index

Best regards Martin

CodePudding user response:

Python variable names cannot start with digits, so I will call the lists list1 and list2. You need a function to check how similar two coordinate pairs are. A simple function is to take the sum of the absolute values of the differences of the two x values and two y values, so if the two coordinate pairs are (x1, y1) and (x2, y2) then the function should output abs(x1-x2) abs(y1-y2) This function gives the difference between two coordinate pairs, so you want the pair with the minimum - min() - difference The python min() function accepts an argument called key, where we can enter this function. So code that works is:

list1 = [[0.1, 0.7], [0.2, 0.2], [0.5, 0.2], [0.5, 0.6], [0.7, 0.2], [0.8, 0.7]]
list2 = [[0.5, 0.7], [0.5, 0.1], [0.2, 0.7], [0.8, 0.2], [0.8, 0.7], [0.2, 0.2]]
#relative_list2 = list with same len() as list2

sorted_list2 = []
sorted_relative_list2 = []
for coords in list1:
    match = min(list2, key=lambda n: abs(n[0]-coords[0]) abs(n[1]-coords[1]))
    sorted_list2.append(match)
    sorted_relative_list2.append(relative_list2[list2.index(match)])

print(list1)
print(sorted_list2)
print(sorted_relative_list2)

This outputs what you want (the sorted list_2 is in the variable sorted_list2)

  • Related