以下代码获取匹配值的索引:
x = ["Moon", "Earth", "Jupiter", "Neptune", "Earth", "Venus"]
get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
print(get_indexes("Earth", x))
以下代码提取满足条件的峰值:
intensitySortedPeaks = sorted(peaks, key=lambda p: p.Intensity, reverse=True)
for i in range(len(intensitySortedPeaks)):
testMass = intensitySortedPeaks[i].MZ + 1.0033
results = [p for p in intensitySortedPeaks if abs(GetPPMError(p.MZ,testMass)) < 10]
###how do we get the indexes saved as indeResults
如何获取存储在结果变量中的元素的索引。我想从循环
中的原始intensitySortedList中删除这些元素如何修改上述代码才能完成此任务?
答案 0 :(得分:0)
如果只想要匹配项目的索引,请尝试:
def indices_satisfying(fn, lst):
return [index for index,x in enumerate(lst) if fn(x)]
如果你想删除令人满意的元素吗
def removing_satisfying(fn, lst]:
return [x for x in lst if not fn(x)]