赞
踩
我有一个list容器,其中存放的是结构体数据,数据格式如下
- class TestID:
- def __int__(self):
- self.id = -1
- self.similary = 0.0
排序的格式:相似度从大到小,ID号从大到小
意思是:当相似度相同时,ID号再进行排序
那么,我的初始化数据如下所示,仅做参考样本数据
- arr_list = []
-
- st = TestID()
- st.id = 10
- st.similary = 0.88
- arr_list.append(st)
-
- st = TestID()
- st.id = 8
- st.similary = 0.78
- arr_list.append(st)
-
- st = TestID()
- st.id = 9
- st.similary = 0.88
- arr_list.append(st)
-
- st = TestID()
- st.id = 1
- st.similary = 0.5
- arr_list.append(st)
-
- st = TestID()
- st.id = 2
- st.similary = 1
- arr_list.append(st)
我列举的结构体中有两个相似度相同(0.88),ID不同的数据
排序方法:
- def __sortTestSTGroup(self , testST ):
- length = len(testST )
- for i in range(1, length):
- x = testST [i]
- x_similary = testST [i].similarity
- x_id = testST [i].id
- for j in range(i, -1, -1):
- # j为当前位置,试探j-1位置
- temp_id = testST [j - 1].id
- temp_similary = testST [j - 1].similarity
- if x_similary == temp_similary:
- if x_id < temp_id:
- testST [j] = testST [j - 1]
- else:
- break
- elif x_similary > temp_similary:
- testST [j] = testST [j - 1]
- else:
- # 位置确定为j
- break
- testST [j] = x
- # for item in testST :
- # str_los = '改变后的内容:id=%s,similarity=%f' % (x.id, x.similarity)
- # print(str_los)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。