How to sort a list of strings numerically?

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

    If you want to use strings of the numbers better take another list as shown in my code it will work fine.

    list1=["1","10","3","22","23","4","2","200"]
    
    k=[]    
    for item in list1:    
        k.append(int(item))
    
    k.sort()
    print(k)
    # [1, 2, 3, 4, 10, 22, 23, 200]
    

提交回复
热议问题