Python: How to custom order a list?

后端 未结 4 1135
北荒
北荒 2020-12-10 15:10

Obs: I know lists in python are not order-fixed, but think that this one will be. And I\'m using Python 2.4

I have a list, like (for example) this one:



        
4条回答
  •  渐次进展
    2020-12-10 15:24

    You could use a dictionary that would map every first element to its "weight" and then check this dictionary inside a sorting function.

    Something like:

    d = { "Report": 1,
          "Article": 2,
           "Book": 3 }
    result = sorted(mylist, key=lambda x:d[x[0]])
    

提交回复
热议问题