在python中嵌套for循环,似乎无法使其工作

2024-10-16 22:31:46 发布

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

我试图将一个列表中的一个字符串插入字符串匹配的另一个列表中(列表的长度不同),但我似乎不知道如何开始工作。所以我在两个列表上循环,在一个字符串匹配中,我将列表2中的下一行插入到列表1中,这需要继续,直到列表2中的所有行都插入到列表1的正确位置。这是我现在掌握的密码

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**

清单1中的项目

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的字符串应该是荷兰语,但我还在翻译


Tags: nonamecodef2f1f130p4960t14
1条回答
网友
1楼 · 发布于 2024-10-16 22:31:46

基本上,只有当来自l2的元素与来自l2的下一个元素相同时,才希望在l3(这是l1)中插入来自l2的元素?如果是,请尝试:

import more_itertools as mit


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

我的解决方案基于来自more_itertoolspairwise函数pairwise迭代列表中的每一对后续元素

相关问题 更多 >