3

I have a list as

a = [["1", "ok", "na"], ["15", "asd", "asdasd"], ["100", "uhu", "plo"], ["10", "iju", "tlo"], ["ISC_1", "des", "det"], ["12", "asd", "assrg"], ["ARF", "asd", "rf"]]

I want this list to be sorted as below:

[['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['100', 'uhu', 'plo'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']]

I have used a.sort()

It is resulting as below:

[['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['100', 'uhu', 'plo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']]

Please help me how to sort in this case.

2

3 Answers 3

6

You can use the key named argument.
It accepts a function that returns the value the sorting function should compare items by.

sorted(a, key = lambda l: int(l[0]))
Sign up to request clarification or add additional context in comments.

8 Comments

Will it work in python2.7 ? getting below error >>> sort(a, key = lambda l: int(l[0])) Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> sort(a, key = lambda l: int(l[0])) NameError: name 'sort' is not defined
with ´a.sort(key = lambda l: int(l[0]))´ or ´b=sorted(a,key = lambda l: int(l[0]))´
what if the case my list is a = [["1", "ok", "na"], ["15", "asd", "asdasd"], ["100", "uhu", "plo"], ["10", "iju", "tlo"], ["ISC_1", "des", "det"]]
It is throwing invalid literal for int() with base 10: 'ISC_1'
what do you want do happen with ISC? should it come last or first? if it should come last: a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 99999)
|
3

To be ready for non numeric values you can use

a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 99999)
# or
b=sorted(a,key = lambda l: int(l[0]) if l[0].isnumeric() else 99999)

to see non-numeric last or

a.sort(key = lambda l: int(l[0]) if l[0].isnumeric() else 0)
# or
b=sorted(a,key = lambda l: int(l[0]) if l[0].isnumeric() else 0)

to see them first

1 Comment

Thanks for the response. I want the result as [['1', 'ok', 'na'], ['10', 'iju', 'tlo'], ['12', 'asd', 'assrg'], ['15', 'asd', 'asdasd'], ['100', 'uhu', 'plo'], ['ARF', 'asd', 'rf'], ['ISC_1', 'des', 'det']] With ARF first and ISC_1 last
3

You can use a natural sorting key, very easy to setup with the regular expression re.split()

import re
try:
    # fast string checking and conversion
    from fastnumbers import *
except:
    pass

def natural_sort_key_for_list_of_lists(sublist):
    return [int(element) if element.isdigit() else element
            for element in re.split("([0-9]+)",sublist[0])]
    # put whichever index of the sublist you want here ^

a = [["1", "ok", "na"],
     ["15", "asd", "asdasd"],
     ["100", "uhu", "plo"],
     ["10", "iju", "tlo"],
     ["ISC_1", "des", "det"],
     ["12", "asd", "assrg"],
     ["ARF", "asd", "rf"]]

a.sort(key=natural_sort_key_for_list_of_lists)

for l in a:
    print (l)

result:

['1', 'ok', 'na']
['10', 'iju', 'tlo']
['12', 'asd', 'assrg']
['15', 'asd', 'asdasd']
['100', 'uhu', 'plo']
['ARF', 'asd', 'rf']
['ISC_1', 'des', 'det']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.