📌  相关文章
📜  python 按自定义顺序对列表进行排序 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:00.777000             🧑  作者: Mango

代码示例1
# Example usage:
list_to_sort = [('U', 23), ('R', 42), ('L', 17, 'D')]
custom_sort_order = ['R', 'D', 'L', 'U']
sorted(list_to_sort, key=lambda list_to_sort: custom_sort_order.index(list_to_sort[0]))
# Where 0 is the tuple index to use for sorting by custom order
--> [('R', 42), ('L', 17, 'D'), ('U', 23)]