Sometimes, while working with Python Matrix, we can have data arranged randomly and we can have a requirement in which we need to get the element position in sorted order of Matrix. Let’s discuss a certain way in which this task can be performed.
Method : Using list comprehension +
enumerate() + sort() + lambda
The solution to problem can be achieved using the combination of above functions. In these, we need to first arrange element for index value tuple creation using
enumerate() and list comprehension. Then, we employ sort function to perform custom sort using lambda function.
The original list is : [[1, 3, 1], [4, 6], [7, 8, 10]] Sorted order of Matrix elements : [(0, 0), (0, 2), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.