首页 > 解决方案 > 列表未按正确顺序附加

问题描述

在这段代码中,我试图将基因 ID 的基因描述附加到前 50 个 R 值。我得到了两个列表 top50 和geneanno。

列表 top50 看起来像[0.5218951111466706, 'AHY40589'] 和列表 geneanno 看起来像['AHY39293', 'Disulfide bond formation protein DsbB\n']

我下面的代码显示了我如何附加列表,但我似乎无法附加与基因 ID 对应的正确基因描述。

    r_sorted = sorted(r_value)
unsorted = []
for s in r_sorted:
  if s not in unsorted:
    unsorted.append(s)
top50 = unsorted[::-1][:50]                           #top 50 with highest correlation
print(top50)

annotation = []
path = '/content/drive/abc_gene_anno.txt'
with open(path,'r') as f:
  geneanno = [l.split('\t') for l in f] #obtain gene ids
  for d in geneanno:
    for c in top50:
      if c[1] in d[0]:
        annotation.append(d[1][:-1])
print(d)
print(c)
print(annotation)

输出是

[[0.9999999999999999, 'AHY39286'], [0.939173984187146, 'AHY39284']]
['AHY39293', 'Disulfide bond formation protein DsbB\n']
[0.5218951111466706, 'AHY40589']
['Cysteine-rich domain', 'Cysteine-rich domain', 'FdhD/NarQ family', 'Protein of unknown function (DUF979)']

例如,使用具有最高 r 值的 Gene ID (AHY39286), 这是基因描述

但是,我的代码改为打印在此处输入图像描述

标签: pythonlistfor-loopif-statementappend

解决方案


尝试更改循环的顺序

  for c in top50:
    for d in geneanno:

目前,annotation文件中出现的相关键的顺序


推荐阅读