按每个int的第一位对int列表进行排序 [英] Sort list of ints by the first digit of each int
本文介绍了按每个int的第一位对int列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何按每个int的第一个数字对整数列表进行排序(如果相同,则移至下一个数字,依此类推)
I'm trying to figure out how to sort a list of integers by the first digit in each int, (and if the same, move to the next digit, etc.)
我确定我可以循环遍历,(尽管我一直遇到问题,因为为了获取第一个数字,我似乎需要将我的列表包含在字符串列表中,但这一直没有起作用但我想知道是否有一种方法可以通过sorted()
方法轻松地做到这一点.
I'm sure I can just loop through, (although I've been having issues because it seems like I need to make my list a list of strings in order to grab the first digit and this just hasn't been working out for me), but I'd like to know if there is a way to do this easily with the sorted()
method.
EX:
myList = [34254, 2343, 49, 595, 323]
我想要的结果:
sortedList = [2343, 323, 34254, 49, 595]
推荐答案
使用一串字符串进行排序,您将获得ASCIIbetical排序.
Sort with a key of strings and you'll get ASCIIbetical sorting.
>>> myList = [34254, 2343, 49, 595, 323]
>>> sorted(myList, key=str)
[2343, 323, 34254, 49, 595]
这篇关于按每个int的第一位对int列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文