【问题标题】:How do I not print the unwanted products of a nested loop?如何不打印嵌套循环的不需要的产品?
【发布时间】:2021-06-18 06:03:32
【问题描述】:

我有一个包含许多行的文件,如下所示:-

(PTQ<38>_1:0.199472,(AagrBONN<35>*_0:0.247985,(((GBG<27>_0:0.357611,(Vocar<21>_1:0.91073,Klenit<20>_2:0.326442)<26>_1:0.070751)<31>_1:0.044341,(ME<25>_0:0.3226,SM<24>_0:0.318938)<30>_1:0.054235)<33>_1:0.094663,(EFJ<29>_3:0.314696,(((AmTr<15>_8:0.147156,((Zm<5>*_22:0.077246,Os<4>*_13:0.071153)<9>_16:0.173488,(VIT<3>_16:0.086305,(AT<1>*_10:0.182135,Potri<0>*_24:0.095723)<2>_15:0.025874)<8>_15:0.051653)<14>*_13:0.038202)<19>*_10:0.092418,(TnS<13>_7:0.205925,(PILA<7>_10:0.171663,SEGI<6>*_4:0.166892)<12>_7:0.020503)<18>_7:0.040364)<23>_7:0.091012,(Cericv<17>*_12:0.154953,(Azfi<11>_7:0.103752,Sacu<10>_10:0.11457)<16>_8:0.059988)<22>_8:0.123914)<28>*_6:0.036195)<32>*_3:0.024392)<34>_2:0.01915)<37>_2:0.028257,Pp<36>_2:0.235806)<39>_2;

PTQ 和 AagrBONN 之类的字母是物种名称的缩写,我有第二个文件,其中包含以下信息(各个缩写的全名):-

AT  Arabidopsis thaliana
AagrBONN    Anthoceros angustus
AmTr    Amborella trichopoda
Azfi    Azolla filiculoides
Cericv  Ceratopteris richardii
EFJ Selaginella moellendorffii
GBG Chara braunii
Klenit  Klebsormidium flaccidum
ME  Mesotaenium endlicherianum
Os  Oryza sativa
PILA    Pinus lambertiana
PTQ Marchantia polymorpha
Potri   Populus trichocarpa
Pp  Physcomitrella patens
SEGI    Sequoiadendron giganteum
SM  Spirogloea muscicola
Sacu    Salvinia cucullata
TnS Gnetum montanum
VIT Vitis vinifera
Vocar   Volvox carteri
Zm  Zea may 

对于第一个文件中的每一行,我需要用全名替换缩写。我试图用以下方法解决这个问题:-

使用第二个文件,我制作了一个字典,其中缩写作为键,全名作为值,将这个字典保存到一个 pickle 文件中,并编写了一个代码来循环第一个文件中的行和字典中的键并用它们各自的配对值(这里是全名)替换键的所有正则表达式匹配(这里是缩写)。

我目前的代码如下:-

from os import replace
import pickle
import re

def main():

    
    with open('species_dict.pkl','rb') as handle:
        the_dict = pickle.load(handle)

    with open('Base_asr.tre','rt') as the_base_file:
        the_base_file_line = the_base_file.readlines()

    for the_line in the_base_file_line:
        for the_key in the_dict:
            x = the_line.replace(the_key,the_dict[the_key])
            print(x)
        #print(x) ??    

main()

dict 只是第二个文件中的 tsv 格式。

问题是我在第一个文件中有 216 行,在 dict 中有 20 个条目,当我打印(x)时,我最终得到 216 * 20 行,对于 216 * 20 行中的每一行,只有一个缩写是每行替换。我正在尝试找到一种仅打印 216 行的方法。

我尝试更改 print(x) 并将其分配给第一个 for 循环,希望在每行循环每个 dict 键后 x 会打印,但这不起作用并且做了一些我无法真正理解的事情。

我知道这是因为我的嵌套循环的性质而发生的,我在概念上迷失了如何获得 216 条正确的行而不是 216 * 20 条半断行。

我该怎么办?

【问题讨论】:

    标签: python logic nested-loops


    【解决方案1】:

    在内部循环中,每次迭代都使用原始的the_line,因此您永远不会替换所有值。您替换一个值,然后从 the_line 的新版本开始并替换第二个值,等等。

    如果你想保持相同的基本代码格式,你应该在每次迭代中覆盖原来的the_line以捕获所有替换:

    the_line = the_line.replace(the_key,the_dict[the_key])
    

    现在在第一次迭代后the_line 将是第一个替换的原始值,然后下一次迭代将其分配给第二个,等等......

    内循环外你可以print(the_line)

    FWIW,您可能可以使用单个正则表达式处理每一行:

    import re
    for the_line in the_base_file_line:
        x = re.sub(r'\w+(?=<)', lambda m: the_dict[m.group()], the_line)
        print(x)
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2020-02-13
      相关资源
      最近更新 更多