2014-11-06 49 views
0

我正在使用一种算法,该算法使用肽的排行榜并删除没有特定“分数”的肽。我目前正在努力理解为什么我不断收到有关我的删除过程的错误。我相信它可能必须与我的复制清单,然后我在我的清除过程中使用。不过,我在前面的问题中使用了相同的过程,没有任何问题,所以我不明白为什么这个特定的实例抛出错误。蟒蛇 - 项目不在列表中(但它是) - 复制列表

def Trim(Leaderboard,Spectra,N): 
    Scores=[];droplist=[] 
    for peptide in Leaderboard: 
     Scores.append(LinearScore(peptide,Spectra)) 
    Leaderboard,Scores = zip(*sorted(zip(Leaderboard, Scores),key=lambda peptide: peptide[1], reverse=True)) 
    Leaderboard=list(Leaderboard);Scores=list(Scores) ### IS THIS WHERE THE PROBLEM IS???? 
    Cutoffscore=Scores[N-1] # Here I am finding the Score of the Nth' peptide (in sorted order) 
    for peptide,score in zip(Leaderboard,Scores): # iterate through list of tuples 
     if score<Cutoffscore: # if peptide score is lower than cut off score 
      droplist.append(peptide) # remove that peptide from the leaderboard 
    for i in droplist: 
     Leaderboard.remove(i) ### ERROR THROWN HERE "Error list.remove(x), x not in list" 
    return Leaderboard # then return what's left of the list 

编辑:问题是在程序

+3

你可以发布数据的片段,使人们可以测试? – 2014-11-06 23:51:20

+0

您是否有可能删除已删除的内容? – 2014-11-06 23:55:02

+0

你可以'添加多肽'你*想保留*到一个新的列表?或者你必须删除它们?你可能需要小心以'list'作为输入参数,然后返回它,你听说过修改'in place'吗?你可以看到一些你不期望的行为。在代码中分隔不同位的代码中还有几行空白行看起来不错:P我仍然这样做。 – 2014-11-06 23:55:20

回答

1
new_list = [peptide for peptide,score in zip(Leaderboard,Scores) if score >= CutoffScore] 

位于其他地方是实现一个更漂亮的方式......这是说你应该遍历droplist向后

for i in reversed(droplist): 
    Leaderboard.remove(i) 

的问题是,

考虑你的情况,你有分数[1,5,5,5,5,5,1] ...哟UR在droplist指标是[0, 6]

然而,一旦你弹出0 ...没有索引6 ...您的其他指标已经转移到5

+0

你的第一个代码块(list = [peptide for peptide ...] ),如果我使用的话,我甚至需要有一个下拉列表吗? – Adam 2014-11-07 00:17:32

+0

没有创建一个新的列表只有比你的截止点得分更好的肽... – 2014-11-07 01:23:22