sort string list by another string list python

Question:

5 downvotes for what? like seriously wtf?

I asked a clear question and got a clear answer from Barmer.
Now i cant ask another question and now im forced to create a new stack overflow account and dont suggest that i dont cause your system is shit so yes i 100% absolutely will create a new one. and another one, and another one.

Eat shit.

So i have 2 lists.

list1 is the list of which order im trying to recreate.

list2 is the unordered list.

How to arrange list2 in same order as list1?

List1

list1 = [
            "loaded_sound,sfx/weapon/smg/type100/fire",
            "loaded_sound,sfx/weapon/smg/type100/foley",
            "loaded_sound,sfx/weapon/special/bouncing_betty",
            "loaded_sound,sfx/weapon/special/flame_thrower/foley",
            "loaded_sound,sfx/weapon/special/molotov",
            "loaded_sound,sfx/weapon/special/ptrs/fire",
            "loaded_sound,sfx/weapon/special/satchel_charge",
            "loaded_sound,sfx/weapon/tank/kingtiger/fire",
            "loaded_sound,sfx/weapon/tank/ringoffs",
            "loaded_sound,sfx/weapon/tank/sherman/fire",
            "loaded_sound,sfx/weapon/tank/tank_reload/foley",
            "loaded_sound,sfx/weapon/tank/tank_reload",
            "loaded_sound,sfx/weapon/tesla/bounce",
            "loaded_sound,sfx/weapon/tesla",
            "loaded_sound,sfx/weapon/uber",
            "loaded_sound,stream/music/mission/zombie",
            "loaded_sound,voiceovers/zombie/ann",
            "loaded_sound,voiceovers/zombie/monkey/explo_vox",
            "loaded_sound,voiceovers/zombie/monkey/groan",
            "loaded_sound,voiceovers/zombie/monkey",
            "loaded_sound,voiceovers/zombie/monkey/raise_vox",
            "loaded_sound,voiceovers/zombie/pa"
        ]

List2:

list2 = [
            "loaded_sound,sfx/weapon/smg/type100/fire",
            "loaded_sound,voiceovers/zombie/pa",
            "loaded_sound,sfx/weapon/smg/type100/foley",
            "loaded_sound,voiceovers/zombie/monkey/raise_vox",
            "loaded_sound,sfx/weapon/special/bouncing_betty",
            "loaded_sound,sfx/weapon/special/flame_thrower/foley",
            "loaded_sound,voiceovers/zombie/monkey",
            "loaded_sound,voiceovers/zombie/monkey/groan",
            "loaded_sound,sfx/weapon/special/molotov",
            "loaded_sound,voiceovers/zombie/monkey/explo_vox",
            "loaded_sound,sfx/weapon/special/ptrs/fire",
            "loaded_sound,voiceovers/zombie/ann",
            "loaded_sound,sfx/weapon/special/satchel_charge",
            "loaded_sound,stream/music/mission/zombie",
            "loaded_sound,sfx/weapon/tank/kingtiger/fire",
            "loaded_sound,sfx/weapon/uber",
            "loaded_sound,sfx/weapon/tank/ringoffs",
            "loaded_sound,sfx/weapon/tesla",
            "loaded_sound,sfx/weapon/tank/sherman/fire",
            "loaded_sound,sfx/weapon/tesla/bounce",
            "loaded_sound,sfx/weapon/tank/tank_reload/foley",
            "loaded_sound,sfx/weapon/tank/tank_reload",
        ]

What ive tried:

_newList = []
# sort list2 in same order as list 1
for i in list1:
    _line_ = i
    for j in list2:
        if j == _line_:
            _newList.append(j)

But all this did was recreate list2 as it is. it didnt rearrange anything.

Asked By: phil

||

Answers:

I suggest you the following one-line style code:

list2 = [i for i in list1 if i in list2]
Answered By: Laurent H.

You can use the index in list1 as the key when sorting list2.

If the lists are long, it would be good to create a dictionary that returns the indexes, rather than searching for each.

list1_dict = {val: i for i, val in enumerate(list1)}
list2.sort(key = lambda x: list1_dict.get(x, -1))

If there are any elements in list2 that aren’t in list1, this will sort them to the beginning.

Answered By: Barmar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.