首页 > 解决方案 > 在python中嵌套for循环,似乎无法正常工作

问题描述

我正在尝试将一个列表中的字符串插入到字符串匹配的另一个列表中(列表长度不同)我似乎无法弄清楚如何开始工作。因此,我遍历两个列表,在字符串匹配中,我将 list2 中的下一行插入到 list1 中,这需要继续,直到列表 2 中的所有行都插入到 list1 中的正确位置。这是我现在拥有的代码。

with open('D:\\TranslateFIles\\Ext_Python.txt', 'r', encoding='utf-8') as f:
    list1 = f.readlines()
f.close()

with open('D:\\TranslateFIles\\Combo_Python.txt', 'r', encoding='utf-8') as f:
    list2 = f.readlines()
f.close()

outFile=open('D:\\TranslateFIles\\Output3_Python.txt', 'w', encoding='utf-8')

list3 = list1.copy()


for element, i in enumerate(list3):
        for j,b in zip(list2, list2[1:]):
            if i == j: 
                list3.insert(element, b)
                print(*list3) 
            
for line in list3:
    outFile.write(line) 
outFile.close()

这是我现在得到的输出。

T14-P2818-L30:Location
**T14-P8629-A2067-L999:Vestiging**
T14-P8629-A1033-L999:Location
T14-P8629-A2060-L999:Magasin
T14-P4960-V1000-P2818-L128:TransferRoute
T14-P4960-V1003-P2818-L128:WhseEmployee
T14-P4960-V1004-P2818-L128:WorkCenter
T14-P4960-V1001-P2818-L128:StockkeepingUnit
T14-P4960-X1-L999:
T14-F1-P2818-L128:Code
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2060-L999:Code
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2060-L999:Nom
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2060-L999:Code emplacement par d‚faut

这是我想要得到的输出。

T14-P2818-L30:Location
T14-P8629-A1033-L999:Location
T14-P8629-A2060-L999:Magasin
**T14-P8629-A2067-L999:Vestiging**
T14-P4960-V1000-P2818-L128:TransferRoute
T14-P4960-V1003-P2818-L128:WhseEmployee
T14-P4960-V1004-P2818-L128:WorkCenter
T14-P4960-V1001-P2818-L128:StockkeepingUnit
T14-P4960-X1-L999:
T14-F1-P2818-L128:Code
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2060-L999:Code
**T14-F1-P8629-A2067-L999:Code**
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2060-L999:Nom
**T14-F2-P8629-A2067-L999:Naam**
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2060-L999:Code emplacement par d‚faut
**T14-F130-P8629-A2067-L999:Standaard opslaglocatiecode**

清单一中的项目

T14-P2818-L30:Location
T14-P8629-A1033-L999:Location
T14-P8629-A2060-L999:Magasin
T14-P4960-V1000-P2818-L128:TransferRoute
T14-P4960-V1003-P2818-L128:WhseEmployee
T14-P4960-V1004-P2818-L128:WorkCenter
T14-P4960-V1001-P2818-L128:StockkeepingUnit
T14-P4960-X1-L999:
T14-F1-P2818-L128:Code
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2060-L999:Code
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2060-L999:Nom
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2060-L999:Code emplacement par d‚faut
T14-F5700-P2818-L128:Name 2
T14-F5700-P8629-A1033-L999:Name 2
T14-F5700-P8629-A2060-L999:Nom 2
T14-F5701-P2818-L128:Address
T14-F5701-P8629-A1033-L999:Address
T14-F5701-P8629-A2060-L999:Adresse
T14-F5702-P2818-L128:Address 2
T14-F5702-P8629-A1033-L999:Address 2
T14-F5702-P8629-A2060-L999:Adresse (2Šme ligne)
T14-F5703-P2818-L128:City
T14-F5703-P8629-A1033-L999:City
T14-F5703-P8629-A2060-L999:Ville
T14-F5704-P2818-L128:Phone No.

清单二中的项目

T14-P8629-A1033-L999:Location
T14-P8629-A2067-L999:Location
T14-F1-P8629-A1033-L999:Code
T14-F1-P8629-A2067-L999:Code
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2067-L999:Name
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2067-L999:Default Bin Code
T14-F5700-P8629-A1033-L999:Name 2
T14-F5700-P8629-A2067-L999:Name 2
T14-F5701-P8629-A1033-L999:Address
T14-F5701-P8629-A2067-L999:Address
T14-F5702-P8629-A1033-L999:Address 2
T14-F5702-P8629-A2067-L999:Address 2
T14-F5703-P8629-A1033-L999:City
T14-F5703-P8629-A2067-L999:City
T14-F5704-P8629-A1033-L999:Phone No.
T14-F5704-P8629-A2067-L999:Phone No.
T14-F5705-P8629-A1033-L999:Phone No. 2
T14-F5705-P8629-A2067-L999:Phone No. 2
T14-F5706-P8629-A1033-L999:Telex No.
T14-F5706-P8629-A2067-L999:Telex No.
T14-F5707-P8629-A1033-L999:Fax No.
T14-F5707-P8629-A2067-L999:Fax No.
T14-F5713-P8629-A1033-L999:Contact
T14-F5713-P8629-A2067-L999:Contact
T14-F5714-P8629-A1033-L999:Post Code
T14-F5714-P8629-A2067-L999:Post Code
T14-F5715-P8629-A1033-L999:County
T14-F5715-P8629-A2067-L999:County

A2067 的字符串应该是荷兰语,但我仍在翻译。

标签: pythonfor-loopnested-loops

解决方案


基本上,您只想插入一个元素l3(它是 的副本l1),前提是l2该元素与 froml2的下一个元素相同l2。如果是这样,请尝试:

import more_itertools as mit


l3 = l1.copy()
for i, j in mit.pairwise(l2):
    if i == j:
        l3.append(i)

我的解决方案基于pairwise. 迭代列表中的每一对后续元素。more_itertoolspairwise


推荐阅读