[英]Sort list of ints by the first digit of each 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.) 我试图弄清楚如何按每个int的第一个数字对整数列表进行排序(如果相同,则移至下一个数字,依此类推)
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. 我敢肯定我可以遍历,(尽管我一直遇到问题,因为似乎我需要将我的列表列为字符串列表以获取第一个数字,但这对我来说还没有解决),但我想知道是否有一种方法可以通过
sorted()
方法轻松地做到这一点。
EX: 例如:
myList = [34254, 2343, 49, 595, 323]
My desired result: 我想要的结果:
sortedList = [2343, 323, 34254, 49, 595]
Sort with a key of strings and you'll get ASCIIbetical sorting. 使用一串字符串进行排序,您将获得ASCIIbetical排序。
>>> myList = [34254, 2343, 49, 595, 323]
>>> sorted(myList, key=str)
[2343, 323, 34254, 49, 595]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.