Horje
python sort a list by a custom order Code Example
python sort a list by a custom order
# 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)]




Python

Related
polynomial regression python Code Example polynomial regression python Code Example
Hashing in python Code Example Hashing in python Code Example
exponential regression python Code Example exponential regression python Code Example
python string format Code Example python string format Code Example
dictionary input from user in python3 Code Example dictionary input from user in python3 Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7