0

I have an ordered list and an unordered list, and I'm attempting to figure out how to sort the ordered_list's order based on the unordered_list's. I tried playing with indexes of each element in both lists but was unsuccessful. So, I'm guessing someone has had similar issues or can tell me what function I need to perform.

unordered_list = ['North_America', 'Europe', 'Asia', 'Antarctica',  'Australia', 'Africa']

ordered_list =   ['Africa.jpg', 'Asia.jpg', 'Antarctica.jpg', 'Australia.jpg', 'Europe.jpg', 'North_America.jpg']

# I want to reorder the list in this manner for this case: [5,4,1,2,3,1]

The desired output:

ordered_list = ['North_America.jpg', 'Europe.jpg', 'Asia.jpg', 'Antarctica.jpg',  'Australia.jpg', 'Africa.jpg']
B A C H A S H
  • 126
  • 1
  • 9

1 Answers1

0

Here is the output:

['North_America.jpg', 'Europe.jpg', 'Asia.jpg', 'Antarctica.jpg',  'Australia.jpg', 'Africa.jpg']

Here is the code:

unordered_list = ['North_America', 'Europe', 'Asia', 'Antarctica',  'Australia', 'Africa']
ordered_list =   ['Africa.jpg', 'Asia.jpg', 'Antarctica.jpg', 'Australia.jpg', 'Europe.jpg', 'North_America.jpg']

corrected_list = []

for x in unordered_list:
    for y in range(len(ordered_list)):
        if x in ordered_list[y]:
            corrected_list.append(ordered_list[y])
            break
            
print(corrected_list)
Joshua
  • 551
  • 4
  • 13