【发布时间】:2017-09-29 10:21:22
【问题描述】:
我有两个列表如下:
name =[A, B , C , D , E , F ]
cls=[1, 2 , 3 , 2 , 4 , 1 ]
score=[0.1, 0.2 , 0.5 , 0.3 , 1 , 0.8 ]
表示A属于1类,得分0.1,B属于2类,得分0.2,以此类推。
我正在寻找一种方法来查找具有相同类的对象,如果该对象的分数小于该类中的另一个对象 (cls),则将其删除。所以,我的预期结果是
name =[C , D , E , F ]
cls =[3 , 2 , 4 , 1 ]
score=[0.5 ,0.3 , 1 , 0.8 ]
name、cls 和 score 是列表类型。如何在 python 中实现它?谢谢
这就是我所做的
name_clean=[]
cls_clean=[]
score_clean=[]
for i in range(len(cls)-1):
cls_i=cls[i]
max_index = -1
for j in range(i+1,len(cls)):
cls_j = cls[j]
if (cls_i==cls_j):
if (score[i]<=score[j]):
max_index=j
else:
max_index=i
if (max_index>=0):
name_clean.append(name[max_index])
cls_clean.append(cls[max_index])
score_clean.append(score[max_index])
else:
name_clean.append(name[i])
cls_clean.append(cls[i])
score_clean.append(score[i])
【问题讨论】:
-
您好,这不是代码补全服务。阅读如何提问:describe the problem and what has been done so far to solve it.
-
我投票结束这个问题,因为 OP 要求其他人做自己的工作。
-
我尝试使用两个 for 循环,但没有成功。因此,我在这里问。为什么近了?
-
请展示你的努力...第一步是使用正确的数据结构。
-
别害怕,需要 5 票才能结束问题。当然,渴望获得 1k 的人会做你的工作以获得一些支持。
标签: python python-2.7 numpy