python - Sort a list where there are strings, floats and integers -


is there way in python sort list there strings, floats , integers in it?

i tried use list.sort() method of course did not work.

here example of list sort:

[2.0, true, [2, 3, 4, [3, [3, 4]], 5], "titi", 1] 

i sorted value floats , ints, , type: floats , ints first, strings, booleans , lists. use python 2.7 not allowed to...

expected output:

[1, 2.0, "titi", true, [2, 3, 4, [3, [3, 4]], 5]] 

python's comparison operators wisely refuse work variables of incompatible types. decide on criterion sorting list, encapsulate in function , pass key option sort(). example, sort repr of each element (a string):

l.sort(key=repr) 

to sort type first, contents:

l.sort(key=lambda x: (str(type(x)), x)) 

the latter has advantage numbers sorted numerically, strings alphabetically, etc. still fail if there 2 sublists cannot compared, must decide do-- extend key function see fit.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -