python - 列表未按正确顺序附加
问题描述
在这段代码中,我试图将基因 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)']
解决方案
尝试更改循环的顺序
for c in top50:
for d in geneanno:
目前,annotation
文件中出现的相关键的顺序
推荐阅读
- javascript - 谷歌 api 客户端库的 maxCDN
- python - 如何将符号表达式转换为 Python 中的浮点数?
- python - ipywidgets 布局,垂直对齐框?
- shell - 使用 ansible 检查文件中的序列
- sql - SQL Server:从一个表中复制新添加的行并自动插入到另一个表中
- google-apps-script - 是否可以在提交 Google 表单时启动 OAuth 2 流程?
- python - 为什么 Python 中会出现 Wordcloud 错误?
- r - 将一列数字转换为日期时间
- mysql - 如何获取至少有 1 个关联行与 sequelize 的行
- javascript - CSS 动画只工作一次,之后就不再工作