Python使用sort和class实现的多级排序功能示例
本文实例讲述了Python使用sort和class实现的多级排序功能。分享给大家供大家参考,具体如下:
# -*- coding:utf-8 -*- import random class Temp: def __init__(self, a, b, c): self.a = a self.b = b self.c = c def __str__(self): return ( ('[a: %d b: %d c: %d]' % (self.a, self.b, self.c))) def cmp1(temp1, temp2): if (temp1.a > temp2.a): return 1; elif(temp1.a == temp2.a): if (temp1.b>temp2.b): return 1; elif(temp1.b == temp2.b): return 0; else: return -1; else: return -1; def cmp2(temp1): return temp1.a temp = [] for i in range(200): a = random.randint(1, 100) b = random.randint(1, 100) c = random.randint(1, 100) temp.append(Temp(a,b,c)) # for i in range(10): # print(temp[i]) # print print # for i in sorted(temp,cmp = cmp1): # print(temp[i]) temp.sort(cmp1) for i in range(200): print(temp[i])
运行结果:
[a: 1 b: 61 c: 91]
[a: 1 b: 62 c: 4]
[a: 1 b: 66 c: 6]
[a: 1 b: 73 c: 8]
[a: 2 b: 24 c: 100]
[a: 2 b: 26 c: 26]
[a: 2 b: 52 c: 90]
[a: 4 b: 69 c: 36]
[a: 4 b: 100 c: 66]
[a: 7 b: 59 c: 10]
[a: 7 b: 97 c: 24]
[a: 8 b: 32 c: 84]
[a: 8 b: 49 c: 90]
[a: 9 b: 13 c: 22]
[a: 9 b: 29 c: 99]
[a: 10 b: 52 c: 11]
[a: 11 b: 2 c: 62]
[a: 11 b: 20 c: 56]
[a: 11 b: 38 c: 17]
[a: 11 b: 72 c: 2]
[a: 12 b: 13 c: 48]
[a: 15 b: 39 c: 24]
[a: 15 b: 64 c: 84]
[a: 16 b: 31 c: 89]
[a: 16 b: 60 c: 15]
[a: 16 b: 68 c: 33]
[a: 17 b: 44 c: 97]
[a: 17 b: 82 c: 41]
[a: 17 b: 96 c: 4]
[a: 19 b: 47 c: 22]
[a: 19 b: 97 c: 91]
[a: 20 b: 35 c: 87]
[a: 20 b: 45 c: 71]
[a: 20 b: 58 c: 45]
[a: 21 b: 5 c: 29]
[a: 21 b: 12 c: 1]
[a: 21 b: 24 c: 84]
[a: 21 b: 27 c: 41]
[a: 21 b: 34 c: 4]
[a: 21 b: 44 c: 38]
[a: 21 b: 63 c: 60]
[a: 21 b: 88 c: 60]
[a: 22 b: 15 c: 39]
[a: 23 b: 6 c: 90]
[a: 23 b: 45 c: 36]
[a: 23 b: 95 c: 92]
[a: 24 b: 44 c: 48]
[a: 24 b: 79 c: 86]
[a: 25 b: 40 c: 24]
[a: 25 b: 80 c: 46]
[a: 25 b: 100 c: 40]
[a: 26 b: 34 c: 74]
[a: 26 b: 79 c: 23]
[a: 27 b: 6 c: 51]
[a: 27 b: 8 c: 8]
[a: 27 b: 28 c: 86]
[a: 27 b: 71 c: 48]
[a: 28 b: 5 c: 9]
[a: 29 b: 11 c: 91]
[a: 29 b: 42 c: 17]
[a: 29 b: 47 c: 21]
[a: 31 b: 80 c: 86]
[a: 32 b: 18 c: 24]
[a: 32 b: 30 c: 29]
[a: 32 b: 81 c: 55]
[a: 33 b: 33 c: 10]
[a: 33 b: 36 c: 83]
[a: 34 b: 11 c: 27]
[a: 34 b: 60 c: 52]
[a: 34 b: 62 c: 45]
[a: 34 b: 72 c: 37]
[a: 35 b: 81 c: 50]
[a: 35 b: 88 c: 3]
[a: 36 b: 11 c: 64]
[a: 36 b: 57 c: 93]
[a: 36 b: 92 c: 2]
[a: 38 b: 58 c: 83]
[a: 39 b: 39 c: 66]
[a: 39 b: 47 c: 15]
[a: 40 b: 13 c: 3]
[a: 40 b: 50 c: 66]
[a: 40 b: 99 c: 71]
[a: 41 b: 14 c: 48]
[a: 42 b: 23 c: 38]
[a: 42 b: 26 c: 30]
[a: 42 b: 32 c: 60]
[a: 42 b: 33 c: 61]
[a: 43 b: 27 c: 53]
[a: 43 b: 66 c: 69]
[a: 43 b: 69 c: 81]
[a: 43 b: 71 c: 51]
[a: 44 b: 8 c: 60]
[a: 44 b: 20 c: 7]
[a: 44 b: 48 c: 51]
[a: 44 b: 59 c: 45]
[a: 45 b: 55 c: 95]
[a: 47 b: 45 c: 25]
[a: 47 b: 79 c: 16]
[a: 48 b: 10 c: 32]
[a: 48 b: 17 c: 17]
[a: 48 b: 80 c: 63]
[a: 49 b: 90 c: 32]
[a: 49 b: 93 c: 54]
[a: 50 b: 14 c: 95]
[a: 50 b: 21 c: 75]
[a: 50 b: 41 c: 24]
[a: 50 b: 54 c: 98]
[a: 50 b: 81 c: 70]
[a: 51 b: 33 c: 62]
[a: 52 b: 53 c: 87]
[a: 53 b: 68 c: 68]
[a: 53 b: 70 c: 74]
[a: 53 b: 74 c: 96]
[a: 54 b: 33 c: 16]
[a: 55 b: 80 c: 74]
[a: 55 b: 88 c: 99]
[a: 55 b: 95 c: 44]
[a: 56 b: 14 c: 81]
[a: 56 b: 70 c: 87]
[a: 57 b: 29 c: 21]
[a: 57 b: 81 c: 51]
[a: 58 b: 79 c: 11]
[a: 58 b: 80 c: 87]
[a: 60 b: 78 c: 100]
[a: 61 b: 82 c: 48]
[a: 62 b: 35 c: 64]
[a: 62 b: 65 c: 24]
[a: 62 b: 99 c: 100]
[a: 63 b: 7 c: 61]
[a: 64 b: 24 c: 86]
[a: 64 b: 41 c: 52]
[a: 64 b: 42 c: 40]
[a: 64 b: 63 c: 92]
[a: 66 b: 9 c: 51]
[a: 66 b: 68 c: 78]
[a: 66 b: 78 c: 30]
[a: 66 b: 94 c: 74]
[a: 67 b: 75 c: 41]
[a: 67 b: 78 c: 78]
[a: 68 b: 39 c: 91]
[a: 68 b: 42 c: 19]
[a: 68 b: 42 c: 63]
[a: 69 b: 15 c: 89]
[a: 69 b: 34 c: 86]
[a: 69 b: 37 c: 11]
[a: 69 b: 44 c: 87]
[a: 69 b: 95 c: 68]
[a: 70 b: 44 c: 98]
[a: 70 b: 79 c: 61]
[a: 71 b: 28 c: 95]
[a: 71 b: 81 c: 38]
[a: 72 b: 92 c: 1]
[a: 74 b: 34 c: 74]
[a: 74 b: 67 c: 72]
[a: 75 b: 4 c: 70]
[a: 76 b: 79 c: 90]
[a: 77 b: 71 c: 73]
[a: 78 b: 19 c: 28]
[a: 79 b: 5 c: 17]
[a: 79 b: 84 c: 100]
[a: 79 b: 89 c: 2]
[a: 79 b: 99 c: 33]
[a: 81 b: 17 c: 70]
[a: 81 b: 95 c: 97]
[a: 83 b: 47 c: 51]
[a: 84 b: 22 c: 68]
[a: 84 b: 45 c: 35]
[a: 84 b: 49 c: 63]
[a: 84 b: 90 c: 89]
[a: 85 b: 41 c: 78]
[a: 85 b: 60 c: 19]
[a: 85 b: 61 c: 84]
[a: 86 b: 37 c: 12]
[a: 86 b: 63 c: 17]
[a: 87 b: 5 c: 7]
[a: 87 b: 53 c: 25]
[a: 87 b: 79 c: 38]
[a: 88 b: 4 c: 90]
[a: 89 b: 8 c: 34]
[a: 89 b: 59 c: 21]
[a: 90 b: 1 c: 5]
[a: 90 b: 50 c: 87]
[a: 91 b: 93 c: 63]
[a: 92 b: 81 c: 47]
[a: 92 b: 89 c: 27]
[a: 92 b: 94 c: 51]
[a: 94 b: 37 c: 71]
[a: 95 b: 25 c: 15]
[a: 95 b: 39 c: 56]
[a: 95 b: 59 c: 39]
[a: 96 b: 9 c: 64]
[a: 96 b: 50 c: 20]
[a: 97 b: 12 c: 45]
[a: 97 b: 24 c: 95]
[a: 97 b: 36 c: 72]
[a: 97 b: 41 c: 40]
[a: 97 b: 80 c: 7]
[a: 99 b: 33 c: 23]
[a: 99 b: 56 c: 16]
[a: 100 b: 51 c: 66]
几点需要说明的:
sort
接受三个默认的参数,一定要理解cmp函数的含义。我的理解:当a>b时,return 1;意思越大的值越往后排- 在
cmp1()
函数中一定要用return a-b
,而前往不要return a>b
.因为在Python中,有a==b的时候的判断,感觉和c++不太一样
PS:这里再为大家推荐一款关于排序的演示工具供大家参考:
在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。