简体   繁体   中英

sort string list by another string list python

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.

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.

I suggest you the following one-line style code:

list2 = [i for i in list1 if i in list2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM