[python] What is the sorted key?

sorted , list.sort()

These two are python's built-in functions that sort the list. It's okay if it's a one-dimensional array because you don't have to think too much about it, but it's annoying when it comes to multiple orders. So, here is the explanation as a method when it is multi-order


	a = [
    print sorted(a, key=lambda x:x[2])

If you write this key = lambda x: x [2], the list will be sorted by index = 2, here by characters. However, I wasn't sure if passing this lambda function would work. Apparently I was still stuck in a language where I couldn't rename old-fashioned functions.


	def f1(data,function):
		p = []
		for i in data:
		return p
	def f2(num):
		return num/2.
	a = f1  #Variable a meaning the function f1
	b = f2  #Variable b meaning the function f2

    print f1(3,f2)    #With this result
	print a(3,b)      #This result is the same

So in sorted, the key wasn't specified to compare the second element, It means that a function that returns the element to be compared was specified.


	sort(a,key=lambda x:x[2])
	    # for i in a:
	    #     key(i) =>Comparison factor

This means that you can even throw in your own elaborate functions.


	def function_x(something):
Something that returns very elaborate and unexpected results
		return result
	a = [
    print sorted(a,key=function_x)

For example, if you create a function that returns the soccer world ranking when you throw a country name and sort it, You can sort by country name and soccer ranking. (No demand) This may be useful and unexpectedly useful.

Recommended Posts

[python] What is the sorted key?
[Python] What is @? (About the decorator)
What is python
What is the python underscore (_) for?
What is Python
What is "mahjong" in the Python library? ??
[Python] What is Pipeline ...
[Python] What is virtualenv
What is wheezy in the Docker Python image?
What is the Linux kernel?
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
What is the interface for ...
What is the Callback function?
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
Python for statement ~ What is iterable ~
What is the X Window System?
What is the default TLS version of the python requests module?
Python> What is an extended slice?
How to judge that the cross key is input in Python3
What to do when the value type is ambiguous in Python?
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
What kind of programming language is Python?
Python learning basics ~ What is type conversion? ~
What is the ETL processing framework clivoa?
[Unix] What is the zombie process / orphan process?
What is the cause of the following error?
Where is the python instantiation process written?
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
[Machine learning] What is the LP norm?
What is namespace
What is copy.copy ()
[Python] What is a formal argument? How to set the initial value
What is the XX file at the root of a popular Python project?
Python is easy
What is Django? .. ..
[Introduction to Python] What is the difference between a list and a tuple?
What is dotenv?
What is POSIX?
What is Linux
[Example of Python improvement] What is the recommended learning site for Python beginners?
What is SALOME?
What is Linux?
What kind of book is the best-selling "Python Crash Course" in the world?
What is hyperopt?
Python is instance
What is Linux
[Introduction to Python] What is the method of repeating with the continue statement?
What is pyvenv
What is the fastest way to create a reverse dictionary in python?
What is __call__
What is Linux
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
What is the difference between `pip` and` conda`?
What is "functional programming" and "object-oriented" in Python?