Sort a list with a custom order in Python

前端 未结 4 1976
别跟我提以往
别跟我提以往 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条回答
  •  伪装坚强ぢ
    2020-12-09 09:13

    Since that is not in aphabetical order I don't think there is one single function that can sort it but what you could do is create a new list and then append. This is kind of a cheap method of doing it; but it gets the job done.

    newlist=[];
    for sub_list in mylist:
         if(sub_list[1] == 'DINT']):
              newlist.append(sub_list);
    
    for sub_list in mylist:
         if(sub_list[1] == 'INT']):
             newlist.append(sub_list);
    
    for sub_list in mylist:
         if(sub_list[1] == 'BOOL']):
                newlist.append(sub_list);
    

提交回复
热议问题