Syntax Behind Sorted(Key=Lambda: ...)

Syntax behind sorted(key=lambda: ...)

key is a function that will be called to transform the collection's items before they are compared. The parameter passed to key must be something that is callable.

The use of lambda creates an anonymous function (which is callable). In the case of sorted the callable only takes one parameters. Python's lambda is pretty simple. It can only do and return one thing really.

The syntax of lambda is the word lambda followed by the list of parameter names then a single block of code. The parameter list and code block are delineated by colon. This is similar to other constructs in python as well such as while, for, if and so on. They are all statements that typically have a code block. Lambda is just another instance of a statement with a code block.

We can compare the use of lambda with that of def to create a function.

adder_lambda = lambda parameter1,parameter2: parameter1+parameter2
def adder_regular(parameter1, parameter2): return parameter1+parameter2

lambda just gives us a way of doing this without assigning a name. Which makes it great for using as a parameter to a function.

variable is used twice here because on the left hand of the colon it is the name of a parameter and on the right hand side it is being used in the code block to compute something.

What is `lambda` in Python code? How does it work with `key` arguments to `sorted`, `sum` etc.?

A lambda is an anonymous function:

>>> f = lambda: 'foo'
>>> print f()
foo

It is often used in functions such as sorted() that take a callable as a parameter (often the key keyword parameter). You could provide an existing function instead of a lambda there too, as long as it is a callable object.

Take the sorted() function as an example. It'll return the given iterable in sorted order:

>>> sorted(['Some', 'words', 'sort', 'differently'])
['Some', 'differently', 'sort', 'words']

but that sorts uppercased words before words that are lowercased. Using the key keyword you can change each entry so it'll be sorted differently. We could lowercase all the words before sorting, for example:

>>> def lowercased(word): return word.lower()
...
>>> lowercased('Some')
'some'
>>> sorted(['Some', 'words', 'sort', 'differently'], key=lowercased)
['differently', 'Some', 'sort', 'words']

We had to create a separate function for that, we could not inline the def lowercased() line into the sorted() expression:

>>> sorted(['Some', 'words', 'sort', 'differently'], key=def lowercased(word): return word.lower())
File "<stdin>", line 1
sorted(['Some', 'words', 'sort', 'differently'], key=def lowercased(word): return word.lower())
^
SyntaxError: invalid syntax

A lambda on the other hand, can be specified directly, inline in the sorted() expression:

 >>> sorted(['Some', 'words', 'sort', 'differently'], key=lambda word: word.lower())
['differently', 'Some', 'sort', 'words']

Lambdas are limited to one expression only, the result of which is the return value.

There are loads of places in the Python library, including built-in functions, that take a callable as keyword or positional argument. There are too many to name here, and they often play a different role.

How is sorted(key=lambda x:) implemented behind the scene?

The key function is executed just once per value, to produce a (keyvalue, value) pair; this is then used to sort and later on just the values are returned in the sorted order. This is sometimes called a Schwartzian transform.

You can test this yourself; you could count how often the function is called, for example:

>>> def keyfunc(value):
... keyfunc.count += 1
... return value
...
>>> keyfunc.count = 0
>>> sorted([0, 8, 1, 6, 4, 5, 3, 7, 9, 2], key=keyfunc)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> keyfunc.count
10

or you could collect all the values that are being passed in; you'll see that they follow the original input order:

>>> def keyfunc(value):
... keyfunc.arguments.append(value)
... return value
...
>>> keyfunc.arguments = []
>>> sorted([0, 8, 1, 6, 4, 5, 3, 7, 9, 2], key=keyfunc)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> keyfunc.arguments
[0, 8, 1, 6, 4, 5, 3, 7, 9, 2]

If you want to read the CPython source code, the relevant function is called listsort(), and the keyfunc is used in the following loop (saved_ob_item is the input array), which is executed before sorting takes place:

for (i = 0; i < saved_ob_size ; i++) {
keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
NULL);
if (keys[i] == NULL) {
for (i=i-1 ; i>=0 ; i--)
Py_DECREF(keys[i]);
if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
PyMem_FREE(keys);
goto keyfunc_fail;
}
}

lo.keys = keys;
lo.values = saved_ob_item;

so in the end, you have two arrays, one with keys and one with the original values. All sort operations act on the two arrays in parallel, sorting the values in lo.keys and moving the elements in lo.values in tandem.

Complex lambda expression as the key argument to sorted function

Tuples are compared and sorted in lexicographical order:

>>> sorted([(0, 17), (1,15), (0,12), (0, 9), (1, 8), (1, 7), (0, 2)])
[(0, 2), (0, 9), (0, 12), (0, 17), (1, 7), (1, 8), (1, 15)]
>>> (0, 12) < (1, 9)
True
>>> (0, 12) < (0, 13)
True
>>> (1, 12) < (0, 9)
False

"Lexicographical order" is just a fancy word for "like words in an English dictionary": compare the first letter first, and only if the first letter is equal, then compare the second letter, and only if the first two letters are equal, then compare the third letter, etc.

Using a tuple as the key, the tuples will also be compared in lexicographical order.

In your case, x is None and x is not None evaluate to boolean values, True or False.

Boolean values can be compared too:

>>> False < True
True
>>> True < False
False

As a result, sorted([3, 1, None], key=lambda x: (x is None, x)) will consider that the smallest elements are those for which x is None is False, and the biggest elements are those for which x is None is True:

>>> sorted([3, 1, None], key=lambda x: (x is None, x))
[1, 3, None]
>>> sorted(map(lambda x: (x is None, x), [3, 1, None]))
[(False, 1), (False, 3), (True, None)]

Syntax behind min(must_visit, key=lambda x:...)

your lambda function will return what distance(path[-1], x) returns, so the nerest will be the minimum element from must_visit where distance(path[-1], x) is at minimum

equivalent function:

def my_func(x):
return distance(path[-1], x)

and you can use it like :

nerest = min(must_visit, key=my_func)

Python sorted() or its alternative to get/return resulting key lambda value by which it was sorted

You could already map it before sorting:

>>> sorted(map(lambda x: (temp(x[1]), x), dict_a))
[(1, ('c', 1)), (4, ('b', 2)), (9, ('a', 3))]
>>>

Or with a list comprehension:

>>> sorted([(temp(x[1]), x) for x in dict_a])
[(1, ('c', 1)), (4, ('b', 2)), (9, ('a', 3))]
>>>

How does the key argument in python's sorted function work?

The function you pass in to key is given each of the items that are being sorted, and returns a "key" that Python can sort by. So, if you want to sort a list of strings by the reverse of the string, you could do this:

list_of_strings.sort(key=lambda s: s[::-1])

This lets you specify the value each item is sorted by, without having to change the item. That way, you don't have to build a list of reversed strings, sort that, then reverse them back.

# DON'T do this

data = ['abc', 'def', 'ghi', 'jkl']
reversed_data = [s[::-1] for s in data]
reversed_data.sort()
data = [s[::-1] for s in reversed_data]

# Do this

data.sort(key=lambda s: s[::-1])

In your case, the code is sorting each item by the second item in the tuple, whereas normally it would initially sort by the first item in the tuple, then break ties with the second item.

Explanation needed for sorted(dictionary) using lambda as key

Let's concentrate on the part sorted(english_spanish.items(), key = lambda value: value[1]), because that's what your question is about.
The generator (think of it as a list in this case) english_spanish.items() yields the pairs:

('hi', 'hola')
('thanks', 'gracias')

and so on.
To get them sorted, the sorted function needs to know how to. The key word lambda creates a little anonymous function which gets the pairs (as value) and yields the second entry of each pair. This is what sorted takes as a key to sort upon.



Related Topics



Leave a reply



Submit