tuple排序,按照索引0,1,2,3...依次比较
a = [3,1,0,9,6,2,4,8,7,5] a.sort(key = lambda x: (x%2, x, x%3)) # 先按照x%2的余数排序,再按照数字大小进行排序,再按照x%3的余数排序,优先级依次减低。 >>> a [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
tuple排序,按照索引0,1,2,3...依次比较
a = [3,1,0,9,6,2,4,8,7,5] a.sort(key = lambda x: (x%2, x, x%3)) # 先按照x%2的余数排序,再按照数字大小进行排序,再按照x%3的余数排序,优先级依次减低。 >>> a [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
转载于:https://www.cnblogs.com/buxizhizhoum/p/8521050.html