Sort a list with a custom order in Python

前端 未结 4 1981
别跟我提以往
别跟我提以往 2020-12-09 08:52

I have a list

mylist = [[\'123\', \'BOOL\', \'234\'], [\'345\', \'INT\', \'456\'], [\'567\', \'DINT\', \'678\']]

I want to sort it with the orde

4条回答
  •  Happy的楠姐
    2020-12-09 09:15

    Another way could be; set your order in a list:

    index = [2,1,0]
    

    and create a new list with your order wished:

    mylist = [mylist[_ind] for _ind in indx]
    
    Out[2]: [['567', 'DINT', '678'], ['345', 'INT', '456'], ['123', 'BOOL', '234']]
    

提交回复
热议问题