Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sort with comparator

def mycmp(a, b):
	if a < b:
		return -1
	elif a > b:
		return 1
	return 0
sorted(lst, key=functools.cmp_to_key(mycmp))
# functools.cmp_to_key converts the mycmp to a "key function",
# which returns an object that can be sorted accoding to mycmp
Comment

python sort comparator

sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Comment

PREVIOUS NEXT
Code Example
Python :: slicing of strings in python 
Python :: quantile calcultion using pandas 
Python :: pip change python version 
Python :: concat sort 
Python :: python ceiling division 
Python :: how to give float till 5 decimal places 
Python :: removing duplicates from django models data 
Python :: structure ternaire python 
Python :: python condition question 
Python :: group by pandas 
Python :: while true loop python 
Python :: loop through list of lists jinja 
Python :: python print string and variable 
Python :: downgrade python version windows 
Python :: python prettytable 
Python :: add row to dataframe with index 
Python :: how to sort the order in multiple index pandas 
Python :: python keyboard input 
Python :: relative text size put text cv2 
Python :: python3 call parent constructor 
Python :: chr() function in python 
Python :: django model different schema 
Python :: how to delete record in django 
Python :: session has key python 3 
Python :: how to find python path 
Python :: for loops python 
Python :: gevent with flask 
Python :: django model inheritance 
Python :: To Divide or Not To Divide codechef solution 
Python :: how to drop columns from pandas dataframe 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =