Sorting list based on values from another list?

后端 未结 15 2316
温柔的废话
温柔的废话 2020-11-21 07:14

I have a list of strings like this:

X = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\"]
Y = [ 0,   1,   1,   0,   1,   2,   2,   0,   1 ]
         


        
15条回答
  •  长情又很酷
    2020-11-21 08:16

    list1 = ['a','b','c','d','e','f','g','h','i']
    list2 = [0,1,1,0,1,2,2,0,1]
    
    output=[]
    cur_loclist = []
    

    To get unique values present in list2

    list_set = set(list2)
    

    To find the loc of the index in list2

    list_str = ''.join(str(s) for s in list2)
    

    Location of index in list2 is tracked using cur_loclist

    [0, 3, 7, 1, 2, 4, 8, 5, 6]

    for i in list_set:
    cur_loc = list_str.find(str(i))
    
    while cur_loc >= 0:
        cur_loclist.append(cur_loc)
        cur_loc = list_str.find(str(i),cur_loc+1)
    
    print(cur_loclist)
    
    for i in range(0,len(cur_loclist)):
    output.append(list1[cur_loclist[i]])
    print(output)
    

提交回复
热议问题