How to sort a list of strings numerically?

前端 未结 14 2045
你的背包
你的背包 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

    Python's sort isn't weird. It's just that this code:

    for item in list1:
       item=int(item)
    

    isn't doing what you think it is - item is not replaced back into the list, it is simply thrown away.

    Anyway, the correct solution is to use key=int as others have shown you.

提交回复
热议问题