如何迭代多个列表并仅在存在时打印出来

2024-05-14 02:49:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图迭代几个列表,并找到一个项目是否存在于其他列表中。 这是我的密码:

with open("Busca1.txt", "r") as f, open("CELLO1.txt","r") as f1, open("PSORT.txt","r") as f2, open("results","w+") as of :
    file_in = f.readlines()
    file_in1 = f1.readlines()
    file_in2 = f2.readlines()
    file_in3 = f3.readlines()
    for line in file_in:
        temp = line.split()
        ID_busca = temp[1]
    for line in file_in1:
        temp2 = line.split()
        ID_cello = temp2[1]
    for line in file_in2:
        temp3 = line.split()
        ID_psort = temp3[1]

        all = [i for i in ID_busca if i in ID_cello + ID_psort]
        print all

我得到的是:

['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']
['Y', 'P', '_', '2', '0', '7', '6', '8', '4', '.', '1']


So the item is split by a letter and it seems to be printed out and one item seems to be several times. 

以下是文件示例:

Busca1.txt文件:

C:extracellular YP_207690.1
C:plasma YP_207698.1
C:extracellular YP_207699.1
C:extracellular YP_207700.1
C:extracellular YP_207701.1
C:extracellular YP_207704.1
C:extracellular YP_207706.1
C:extracellular YP_207716.1
C:extracellular YP_207717.1
C:extracellular YP_207719.1
C:plasma YP_207722.1
C:plasma YP_207728.1
C:plasma YP_207729.1
C:extracellular YP_207731.1

CELLO1.txt文件:

OuterMembrane YP_008914846.1 opacity
Periplasmic YP_008914847.1 hypothetical
OuterMembrane YP_008914851.1 opacity
OuterMembrane YP_008914852.1 opacity
OuterMembrane YP_008914853.1 opacity
OuterMembrane YP_008914854.1 opacity
OuterMembrane YP_008914855.1 opacity
OuterMembrane YP_008914857.1 opacity
OuterMembrane YP_008914859.1 opacity
OuterMembrane YP_008914860.1 opacity
Periplasmic YP_008994831.1 hypothetical
Periplasmic YP_009115479.1 DNA
Extracellular YP_009115480.1 bacterioferritin-associated
OuterMembrane YP_009115486.1 pilus
InnerMembrane YP_009115487.1 hypothetical
InnerMembrane YP_009115488.1 membrane
Periplasmic YP_009115490.1 pilin
Periplasmic YP_009179204.1 hypothetical
Periplasmic YP_207190.2 leucine--tRNA

PSORT.txt文件:

SeqID: YP_008914846.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914847.1 hypothetical protein NGO0146a [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914848.1 hypothetical protein NGO0250a [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914849.1 hypothetical protein NGO0590a [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914851.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914852.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914853.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914854.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914855.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914857.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914859.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008914860.1 opacity protein [Neisseria gonorrhoeae FA 1090]
SeqID: YP_008994831.1 hypothetical protein NGO1621a [Neisseria gonorrhoeae FA 1090]
SeqID: YP_009115480.1 bacterioferritin-associated ferredoxin [Neisseria gonorrhoeae FA 1090

有人能帮我把代码弄到我需要的地方吗? 如果在所有当前列表中都存在,我想打印YP*********代码

谢谢


Tags: intxtidlinefilefaproteinhypothetical
1条回答
网友
1楼 · 发布于 2024-05-14 02:49:55

你需要修正你的逻辑。我不知道你是怎么做到的。首先,引用一个不存在的文件f3;你有一个of别名,你打算在那里? 其次,给定的输出不是来自发布的代码。请确保你的问题陈述完全准确

至于预期的操作,请查看循环形式:

for line in file_in:
    temp = line.split()
    ID_busca = temp[1]

仔细阅读文件的每一行,将其拆分,提取ID号,然后用最新的ID号覆盖以前的ID号。当您退出这个循环时,ID_busca是一个简单的字符串,仅为最后一个ID号。当你到达第二个循环的末端时,你有

ID_busca = "YP_207731.1"
ID_cello = "YP_207190.2"

现在迭代PSORT,依次提取每个ID。让我们看看第一个:

ID_psort = "YP_008914846.1"

现在,您的列表理解将逐步遍历ID_busca的每个字符,以查看该字符是否在其他两个字符串中

all = [i for i in ID_busca if i in ""YP_207190.2YP_008914846.1"]

学会一次编写少量代码。在你知道你有要搜索的ID列表之前,不要尝试搜索你的ID。使用print语句。不要再写代码了,除非你写的所有东西都经过了测试并且按你的意愿工作。您发布的代码至少有四个错误

如果你想要一个ID号列表,那么就制作一个ID号列表:搜索关于列表和appendextend方法的在线教程。同时查找sets;我想最简单的方法就是做三组身份证号码,然后简单地取它们的交叉点

相关问题 更多 >