Can I sort text by its numeric value in Python?

后端 未结 6 1937
轮回少年
轮回少年 2020-12-16 01:26

I have dict in Python with keys of the following form:

mydict = {\'0\'     : 10,
          \'1\'     : 23,
          \'2.0\'   : 321,
          \'2.1\'   : 3         


        
6条回答
  •  攒了一身酷
    2020-12-16 02:02

    As an addendum to Ian Clelland's answer, the map() call can be replaced with a list comprehension... if you prefer that style. It may also be more efficient (though negligibly in this case I suspect).

    sorted(mydict.keys(), key=lambda a: [int(i) for i in a.split('.')])

提交回复
热议问题