Home > database >  Replace specific lines in a list, from another list
Replace specific lines in a list, from another list

Time:10-21

I want to replace lines that start with G0 and G1 in a list; from G0 and G1 from another list. And doing this without changing anything else in the list.

List1= ['G1 F2400 X101.031 Y24512.91 E769.44645', 'G0 F2400 
X48501.031 Y112.91 E769.44645', 'G1 F2400 X5801.031 Y112.91 
E769.44645','G0 F2400 X7801.031 Y112.91 E769.44645', 'G1 F2400 
X1451.031 Y1452.91 E769.44645', 'G0 F2400 X101.031 Y112.91 
E769.44645', 'G1 F2400 X101.031 Y112.91 E769.44645',]    List2=['G1 
X100 Y200 Z0', 'M205 E20', 'M204 E20', 'G0 X150 Y100']

 List2=['G1 F2400 X101.031 Y112.91 E769.44645',
'M204 S5000',
'M205 X30 Y30',
'G0 F15000 X101.29 Y112.675',
'M204 S500',
'M205 X5 Y5',
'G1 F2400 X100.906 Y112.291 E769.46135',
'M204 S5000',
'M205 X30 Y30',
'G0 F15000 X101.163 Y112.053',
'M204 S500',
'M205 X5 Y5',
'G1 F2400 X101.545 Y112.434 E769.47615',
'M204 S5000',
'M205 X30 Y30',
'G0 F15000 X101.797 Y112.191',
'M204 S500',
'M205 X5 Y5',
'G1 F2400 X101.422 Y111.816 E769.49069']

Then after the for loop the given list should be the result

TheListIWant=['G1 F2400 X101.031 Y24512.91 E769.44645',
'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X48501.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X5801.031 Y112.91 E769.44645',
 'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X7801.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X1451.031 Y1452.91 E769.44645',
 'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X101.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X101.031 Y112.91 E769.44645']

theReplacement=[]
for i, x in enumerate(List2):
if (x.startswith("G1") or x.startswith("G0")):
    theReplacement.append(x.replace((List2[i], y for y in List1)

CodePudding user response:

You can do this,

List1 = ['G1 F2400 X101.031 Y112.91 E769.44645', 'M204 S5000', 'M205 X30 Y30', 'G0 F15000 X101.29 Y112.675',
     'M204 S500', 'M205 X5 Y5', 'G1 F2400 X100.906 Y112.291 E769.46135', 'M204 S5000', 'M205 X30 Y30', 
     'G0 F15000 X101.163 Y112.053', 'M204 S500', 'M205 X5 Y5', 'G1 F2400 X101.545 Y112.434 E769.47615',
      'M204 S5000', 'M205 X30 Y30', 'G0 F15000 X101.797 Y112.191', 'M204 S500', 'M205 X5 Y5', 
      'G1 F2400 X101.422 Y111.816 E769.49069']

TheGlist = ['G1 F2400 X101.031 Y24512.91 E769.44645', 'G0 F2400 X48501.031 Y112.91 E769.44645', 
'G1 F2400 X5801.031 Y112.91 E769.44645','G0 F2400 X7801.031 Y112.91 E769.44645', 'G1 F2400 X1451.031 Y1452.91 E769.44645',
 'G0 F2400 X101.031 Y112.91 E769.44645', 'G1 F2400 X101.031 Y112.91 E769.44645'] 

result = []
for idx, i in enumerate(List1):
    if idx % 3 == 0:
        result.append(TheGlist[idx//3])
    else:
        result.append(i)

The oneline approach,

result = [TheGlist[idx//3] if idx % 3 == 0 else i for idx, i in enumerate(List1)]

Output:

['G1 F2400 X101.031 Y24512.91 E769.44645',
 'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X48501.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X5801.031 Y112.91 E769.44645',
 'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X7801.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X1451.031 Y1452.91 E769.44645',
 'M204 S5000',
 'M205 X30 Y30',
 'G0 F2400 X101.031 Y112.91 E769.44645',
 'M204 S500',
 'M205 X5 Y5',
 'G1 F2400 X101.031 Y112.91 E769.44645']

CodePudding user response:

theReplacement=[]
for i, x in enumerate(List2):
    if (x.startswith("G1") or x.startswith("G0")):            
        theReplacement.append(x)
    else:
       theReplacement.append(List1[i])

You can just add one item at a time, and when you are not adding from the second list, you need to add from the first list item (that why I added else statement). You can access elements from first list by index.

  • Related