首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python不在列表(但它是)-Copying列表中。

Python不在列表(但它是)-Copying列表中。
EN

Stack Overflow用户
提问于 2014-11-06 23:47:32
回答 1查看 129关注 0票数 0

我正在研究一种算法,该算法使用的是肽的领导板,并删除没有特定“分数”的肽。目前,我很难理解为什么我在删除过程中不断出错。我相信这可能与我复制名单有关,然后我在删除过程中使用。但是,我在以前的问题中使用了同样的过程,没有问题,所以我不明白为什么这个特定的实例会抛出一个错误。

代码语言:javascript
复制
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

编辑:问题出现在程序的其他地方。

EN

回答 1

Stack Overflow用户

发布于 2014-11-07 00:00:44

代码语言:javascript
复制
new_list = [peptide for peptide,score in zip(Leaderboard,Scores) if score >= CutoffScore]

是一个更好的方法来实现这一点..。也就是说你应该向后遍历液滴

代码语言:javascript
复制
for i in reversed(droplist):
    Leaderboard.remove(i)

问题是

考虑一下你有分数[1,5,5,5,5,5,1]的情况.您在液滴中的索引是[0, 6]

但是一旦你弹出0..。没有指数6。你的另一个指数已经转移到5。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26791604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档