Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python 3 custom sort with compare

# Works with python 3+
def compare(item):
    #First index (index = 1) used for comparison first -> bigger to smaller -> Descending
    #Second index (index = 2) used for comparison after that -> smaller to bigger -> Ascending
    return (-item[1], item[2])

output = [['perfect', 3, 2], ['just', 1, 10], ['get', 1, 6], ['makes', 1, 1]]
output.sort(key=compare)

>> [['perfect', 3, 2], ['makes', 1, 1], ['get', 1, 6], ['just', 1, 10]]
Comment

PREVIOUS NEXT
Code Example
Python :: cartesian product pandas 
Python :: django create object with default today date 
Python :: pandas read excel with two headers 
Python :: Kill python background process 
Python :: python make an object hashable 
Python :: remove keys from array python 
Python :: random.choice 
Python :: try except finally python 
Python :: connect to spark cluster 
Python :: python function get number of arguments 
Python :: square root python 
Python :: python how to delete from dictionary a nan key 
Python :: install python in docker file 
Python :: get index of value in list if value meet condition python 
Python :: discord.py reference 
Python :: list variables in session tensorflow 1 
Python :: try except python not working 
Python :: remove all rows with at least one zero pandas 
Python :: create pytorch zeros 
Python :: execute terminal command from python 
Python :: sort dictionary by value and then key python 
Python :: Return the number of times that the string "hi" appears anywhere in the given string. python 
Python :: how to sum all the numbers in a list in python 
Python :: ppcm python 
Python :: extends template django 
Python :: add column to start of dataframe pandas 
Python :: python pause function 
Python :: replace nan with 0 pandas 
Python :: discord.py edit messages 
Python :: set allowed methods flask 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =