As input I will be getting lists of lists which can up to n-levels and it will vary every time. Suppose, I have a list
[[2, 1, 3], 4, [2, 3], 7, 1, [9, [4, 2], 5]]
here I want to sort this list and expected output is
[1, 4, [2, 3], [1, 2, 3], 7, [5, [2, 4], 9]]
Here, first sorting is happening based of elements and then based on sum of elements inside list.
code:
input_freq = [[2,1,3],4,[2,3],7,1,[9,[4,2],5]]
res = []
def sortFreq(input_freq):
elements = []
list_of_elements = []
for each in input_freq:
if isinstance(each, list):
print "list"
list_of_elements.append(each)
each.sort()
else:
elements.append(each)
elements.sort()
print elements
print list_of_elements
sortFreq(input_freq)
expected output:
[1, 4, [2, 3], [1, 2, 3], 7, [5, [4, 2], 9]]
but my code returns the wrong result:
[[1, 2, 3], [2, 3], [5, 9, [4, 2]]]
[9, [4, 2], 5]? The sum of the flattened elements, so9 + 4 + 2 + 5 == 20?