python - Find index of string in list -


i have 2 lists same number of elements, of them strings. these strings same set in different order in each list no duplicates.

list_a = ['s1', 's2', 's3', 's4', 's5', ...] list_b = ['s8', 's5', 's1', 's9', 's3', ...] 

i need go through each element in list_a , find index in list_b contains same element. can 2 nested loops there has better/more efficient way:

b_indexes = [] elem_a in list_a:     indx_b, elem_b in enumerate(list_b):         if elem_b == elem_a:             b_indexes.append(indx_b)             break 

if there no duplicates, can use list.index():

list_a = ['s1', 's2', 's3', 's4', 's5'] list_b = ['s8', 's5', 's1', 's9', 's3'] print [list_b.index(i) in list_a] 

you need use 1 loop, because you've said strings in list_a appear in list_b, there's no need go if elem_b == elem_a: , iterate through second list.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -