Python sort list of tuples by first value of tuple except for special case -


hey have list of tuples looks like:

[(5, "dummmy_string1"), (6, "dummy_string2"), (3, "special_string")]

i want order in ascending order of first value of tuple, except case string equal special_string. want special_string ordered last regardless of integer value in tuple.

i have right doesn't seem working:

sorted(li, key=lambda x: (x.string_value == "special_string", x.int_value))

i find tuples have special_string, remove them list, sort list, , append them end, looking cleaner solution.

edit:

i made silly mistake , realized solution work...=)

li = [(5, "dummmy_string1"), (6, "dummy_string2"), (3, "special_string")] print sorted(li,key=lambda x:(x[-1]=="special_string",)+x) 

should work fine ... no idea x.string_value (other perhaps attributeerror)

in event there several special_string's sort them in same order other strings (that standard left2right tuple sorting)


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -