Python Using .replace () with list comprehension

1

Can someone tell me if I can make it work:

a = ['<a1>', '<a2>', '<a3>', '', '<a5>']
b = ['<b1>', '<b2>', '<b3>', '', '<b5>']
txt = '<a1> test <a2> test <a3> test <a4> test <a5>'
txt.replace(''.join([i for i in a if i is not '']),''.join([j for j in b if j is not '']))
    
asked by anonymous 15.01.2017 / 05:12

1 answer

2

Try this, it is not with list compression but the logic to adopt will be this:

ab = zip(a, b) # relacionar os elementis das listas
for a_val, b_val in ab:
    txt = txt.replace(a_val, b_val)

link

Or by the indices:

for idx, val in enumerate(a):
    txt = txt.replace(val, b[idx])

link

    
15.01.2017 / 14:22