love Croquembouch
love Croquembouch

Reputation: 63

how can i sort a list of Multidimension?

While studying sort using the key parameter (Lamda x: x), I have a question.

I understand that I can use the key parameter to determine the alignment condition. just like below.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

print(sorted(list1, key=lambda x: -x))
>> [9, 8, 7, 6, 5, 4, 3, 2, 1]

I also know it's possible on a Double brackets list.

list2 = [[1,9], [2,8], [3,7], [4,6], [5,5], [6,4]]

print(sorted(list2, key=lambda x: x[1]))
>> [[6, 4], [5, 5], [4, 6], [3, 7], [2, 8], [1, 9]]

What I'm curious about is, can I set the alignment condition in the Triple brackets list?
For example, how can I sort the list below in ascending order of the third element?

list3 = [[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]], [[5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 1]]]
# possible to sort it like below? in order of third element of Triple square brackets
>> [[[8, 9, 1], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]], [[5, 6, 7], [6, 7, 8], [7, 8, 9]]]

Also, is it possible to sort lists contain element with no brackets, double brackets, and triple brackets?
Something like

list4 = [ 5, 1, 3, [ [[4],[5]], [6] ], [1,2,3], [1,[2,3]]]
>> [ 5, 1, 3, [1,2,3], [1,[2,3]], [ [[4],[5]], [6] ]] # The order is from the lower list.

Upvotes: 0

Views: 35

Answers (1)

pho
pho

Reputation: 25489

When you run sort() on a list, all you do is sort the elements at the top-level of that list.

In list3, the elements at the top-level are themselves lists-of-lists.

list3 = [
          [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]],
          [[5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 1]]
        ]

You can flip the two lines around by calling sorted(list3, key=...), but you cannot change the order of the lists within those lines. To do that, you'll have to iterate over each list in list3 and sort() those lists too.

Remember that sort() doesn't mess with the elements of the list, so it's not possible to call sort and move the [8, 9, 1] element from the second row to the first.


Also, is it possible to sort lists contain element with no brackets, double brackets, and triple brackets?

Yes, so long as you can provide a key function that can take all the top-level list elements and return something that gives the sort order:

list4 = [
           5, 
           1, 
           3, 
           [[[4], [5]], [6]], 
           [1, 2, 3], 
           [1, [2, 3]]
        ]

Your key function needs to be able to take any of the values on each line above as an input. For example, if you were to say that the sort order is given by the largest number in that element regardless of how deep it is, you could do something like this:

# Given a multidimensional list, flatten it into a single dimension
def flatten_list(lst):
    flat_list = []
    for element in lst:
        if isinstance(element, list):
            flat_list.extend(flatten_list(element))
        else:
            flat_list.append(element) 
    return flat_list


def flattened_sort_order(item):
    # If the item is a list, return the max() of the flattened list
    if isinstance(item, list):
        return max(flatten_list(item))
    # Otherwise return the item itself
    else:
        return item

Now, doing sorted(list4, key=flattened_sort_order) gives [1, 3, [1, 2, 3], [1, [2, 3]], 5, [[[4], [5]], [6]]]

Upvotes: 1

Related Questions