How to sort a list of strings numerically?

前端 未结 14 2485
你的背包
你的背包 2020-11-22 03:43

I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of \"numbers\" that are actually in string for

14条回答
  •  我在风中等你
    2020-11-22 04:08

    Try this, it’ll sort the list in-place in descending order (there’s no need to specify a key in this case):

    Process

    listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
    listC = sorted(listB, reverse=True) # listB remains untouched
    print listC
    

    output:

     [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
    

提交回复
热议问题