You can sort the list of lists by length as follows:
l1 = [1,2,3] l2 = [1,2,3] l3 = [1,2] lists = [l1, l2, l3] sorted_lists = sorted(lists, key=len) print sorted_lists #[[1,2], [1,2,3], [1,2,3]]
I cannot figure out how to track indicators, and then map the contents sorted_liststo the names of the original list l1, l2and l3.
sorted_lists
l1
l2
l3
This is approaching, but I'm not sure how the solution can be implemented when sorting by length.
It is very possible. Just change the key a bit to indicate the correct predicate to apply to len.
len
>>> lists = [l1, l2, l3] >>> lists = sorted(enumerate(lists), key=lambda x: len(x[1])) # enumerate is a tuple of (index, elem), sort by len(elem) [(2, [1, 2]), (0, [1, 2, 3]), (1, [1, 2, 3])]
arg.sort() numpy list comprehension :
arg.sort()
numpy
list comprehension
import numpy new_list = [(index, lists[index])for index in numpy.argsort(lists)] print(new_list)
:
[(2, [1, 2]), (0, [1, 2, 3]), (1, [1, 2, 3])]
Source: https://habr.com/ru/post/1679846/More articles:Как вызвать SQL View Dapper С# - c#Removing / adding restrictions programmatically in ConstraintLayout - androidAlign a group of views containing a chain in ConstraintLayout - androidCenter the alignment of a cropped image in a div - htmlBidirectional LSTM with party normalization in Keras - deep-learningБолее полный список состояний принтера CUPS - cupsWhy not> allowed as a Haskell infix operator? - haskellHow to execute multiple queries using MysqlDataReader - c #Python .NET function call from Python - pythonFind intersection of MySQL JSON objects or arrays - jsonAll Articles