Horje
python sort list by custom function Code Example
python sort list by custom function
from functools import cmp_to_key
sorted(mylist, key=cmp_to_key(lambda item1, item2: fitness(item1) - fitness(item2)))
python sort list by custom function
from functools import cmp_to_key
sorted(mylist, key=cmp_to_key(compare))
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
how to use fastapi ApiClient with pytest Code Example how to use fastapi ApiClient with pytest Code Example
how to plot an array in python Code Example how to plot an array in python Code Example
derivative of function in python Code Example derivative of function in python Code Example
david dobrik Code Example david dobrik Code Example
python remove (string) Code Example python remove (string) Code Example

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