Stack Overflow Asked by Rohan Afsan on December 20, 2021
I am trying to sort a nested list in Python in O(n log n) time. I am not sure how to go about it, some help would be appreciated. The inner lists are being sorted by its sum.
My list:
[10, 7, [4,5], [1,2]]
My desired output:
[[1,2], 7, [4,5], 10]
input_list = [10, 7, [4,5], [1,2]]
input_list.sort(key = lambda x: sum(x) if type(x) == list else x)
[[1, 2], 7, [4, 5], 10]
Time complexity will be m*n*lg(n)
n = length of the input list
m = average length of nested list
Answered by ssp4all on December 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP