Reputation: 896
I need to Sort the list according to the integer which are in form of string and list also contains stings alphabets.
li = [['dr','3','mn'],['fs','1','a'],['2','rt',c]]
I need the output like:
li = [['fs','1','a'],['2','rt',c],['dr','3','mn']]
or
li = [[1,'fs','a'],[2,'rt',c],[3,'dr','mn']]
in any of the format like these.
Upvotes: 0
Views: 38
Reputation: 195468
This code supposes the numbers are integers and in each list there's at least one number:
li = [['11','rt','c'],['dr','3','mn'],['fs','1','a'],['2','rt','c']]
def is_number(s):
try:
i = int(s)
return True
except ValueError:
return False
print([li[i[-1]] for i in sorted([[int(j), ii] for ii, i in enumerate(li) for j in i if is_number(j)])])
Prints:
[['fs', '1', 'a'], ['2', 'rt', 'c'], ['dr', '3', 'mn'], ['11', 'rt', 'c']]
Upvotes: 1