How to get an end list for the start?

Asked

Viewed 86 times

4

Opening lists:

B_I = ['Cab', 'Bou', 'Bou', 'RFF', 'RF1', 'Rf2', 'Cor']
Ba_F = ['Bou', 'Zez1', 'Zez2', 'Praca', 'Sro', 'Sro', 'Falag']  

Then I did ........

Final=[] #lista com todos os barramentos sem repetir inicial e final
for a in range (0,len(Ba_I)):
    if (Ba_I[a] not in Final and Ba_I[a]!=0):
        Final.append(Ba_I[a])
for b in range (0,len(Ba_F)):
    if (Ba_F[b] not in Final and Ba_F[b]!=0):
        Final.append(Ba_F[b])
B_I_final = [Final.index(i) for i in B_I]
Ba_F_final = [Final.index(i) for i in Ba_F]

And it gives:

B_I_final = [0, 1, 1, 2, 3, 4, 5]
Ba_F_final = [1, 6, 7, 8, 9, 9, 10]

And now I needed to get the opposite of B_i_final and Ba_f_final..
I tried to make:

b_contr=[] 
for x in B_I_final[::-1]: 
    b_contr.append(B_I_final[x]) 

and give me this for the final B_i_end:

[4, 3, 2, 1, 1, 1, 0] 

and should give:

[5, 4, 3, 2, 1, 1, 0] 

1 answer

6


There are several ways to do it, but I think the fastest way is:

invertido=B_I_final[::-1]

No need to put for nothing, it will be reversed.

Another alternative is to use reverse():

invertido=B_I_final.reverse()

if you want to put inside a for use the function reversed

b_contr=[] 
for i in reversed(B_I_final):
        b_contr.append(i) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.