Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

Insert from one list into another in python

I'm trying to insert from one list into another in python but i cant seem to get it working. I would like to insert the first line of list2 under the first line in list1 that contains 'A1033', then at the next line in list1 that contains 'A1033' insert the second line of list2 and so on. this is the code that i have:

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

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

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

A1033 = 'A1033'
a2067 = 'A2067'

 
for line in list1:
    for line2 in list1:
        for element in list2:
            if A1033 in line:
                if not a2067 in line2:
                    list1.insert(list1.index(line2), element)
                    
            
for lines in list1:
    outFile.write(lines)
outFile.close()   

Snipped of what is in list1

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.

Snipped of list2

T14-P8629-A2067-L999:Locatie
T14-F1-P8629-A2067-L999:Code
T14-F2-P8629-A2067-L999:Naam
T14-F130-P8629-A2067-L999:Standaard opslaglocatiecode
T14-F5700-P8629-A2067-L999:Naam 2
T14-F5701-P8629-A2067-L999:Adres
T14-F5702-P8629-A2067-L999:Adres 2
T14-F5703-P8629-A2067-L999:Stad
T14-F5704-P8629-A2067-L999:Telefoon nr.
T14-F5705-P8629-A2067-L999:Telefoon nr.
T14-F5706-P8629-A2067-L999:Telex Nr.
T14-F5707-P8629-A2067-L999:Fax nr.
T14-F5713-P8629-A2067-L999:Contact
T14-F5714-P8629-A2067-L999:Postcode
T14-F5715-P8629-A2067-L999:County
T14-F5718-P8629-A2067-L999:E-mail
T14-F5719-P8629-A2067-L999:Startpagina
T14-F5720-P8629-A2067-L999:Land/Regio Code
T14-F5724-P8629-A2067-L999:Gebruik als in-transit
T14-F5726-P8629-A2067-L999:Vereisen
T14-F5727-P8629-A2067-L999:Pick
T14-F5728-P8629-A2067-L999:Cross-Dock Vervaldatum Calc.
T14-F5729-P8629-A2067-L999:Cross-Docking gebruiken
T14-F5730-P8629-A2067-L999:Ontvangst vereisen
T14-F5731-P8629-A2067-L999:Verzending vereisen
T14-F5732-P8629-A2067-L999:Bin Verplicht

Snipped of how i want list1 to look after the insert.

T14-P2818-L30:Location
T14-P8629-A1033-L999:Location
T14-P8629-A2067-L999:Locatie
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-A2067-L999:Code
T14-F1-P8629-A2060-L999:Code
T14-F2-P2818-L128:Name
T14-F2-P8629-A1033-L999:Name
T14-F2-P8629-A2067-L999:Naam
T14-F2-P8629-A2060-L999:Nom
T14-F130-P2818-L128:Default Bin Code
T14-F130-P8629-A1033-L999:Default Bin Code
T14-F130-P8629-A2067-L999:Standaard opslaglocatiecode
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-A2067-L999:Naam 2
T14-F5700-P8629-A2060-L999:Nom 2
T14-F5701-P2818-L128:Address
question from:https://stackoverflow.com/questions/66061845/insert-from-one-list-into-another-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Suppose l1 and l2 are you first two list and output is your result then

output = []
for i in l1:
    output.append(i)
    if 'A1033' in i:
        output.append(l2.pop(0))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...