admin管理员组文章数量:1302270
Task: Write a program that sorts a string of numbers received from the user in the following way: Negative numbers come after positive numbers; Both positive and negative numbers are sorted in ascending order.
Input example:
-6 -1 3 -5 -15 4 1 9 8 6 -3 -4 12 -10 7
Output:
1 3 4 6 7 8 9 12 -15 -10 -6 -5 -4 -3 -1
The solution contains the following code:
lst=list(map(int,input().split()))
print(*sorted(lst,key= lambda x: 0 if x==0 else -1/x))
Why are the conditions like that? I especially don't understand "else -1/x"
I just started learning anonymous functions and it's hard to understand them.
Task: Write a program that sorts a string of numbers received from the user in the following way: Negative numbers come after positive numbers; Both positive and negative numbers are sorted in ascending order.
Input example:
-6 -1 3 -5 -15 4 1 9 8 6 -3 -4 12 -10 7
Output:
1 3 4 6 7 8 9 12 -15 -10 -6 -5 -4 -3 -1
The solution contains the following code:
lst=list(map(int,input().split()))
print(*sorted(lst,key= lambda x: 0 if x==0 else -1/x))
Why are the conditions like that? I especially don't understand "else -1/x"
I just started learning anonymous functions and it's hard to understand them.
Share edited Feb 10 at 15:43 no comment 10.6k5 gold badges20 silver badges42 bronze badges asked Feb 10 at 15:22 Kit_kat_rinKit_kat_rin 112 bronze badges 2 |1 Answer
Reset to default 1As juanpa.arrivillaga has mentioned in their comment, the anonymous function lambda x: 0 if x==0 else -1/x
is completely equivalent to the function
def sort_key(x):
return 0 if x == 0 else -1/x
with the only difference being that the lambda expression is constructed on-the-fly within the sorted
expression, and since it has no name, it cannot be referred to outside of the sorted
expression.
Sorting using sorted
with the key
parameter works as follows: key
is a function which takes one argument and is applied to every element of the original list, to obtain a key associated with the element. The order of the elements is determined by sorting the keys, i.e. the comparisons in the sorting algorithm are performed on the keys. The resulting list will then contain the original elements, but in the order obtained by sorting the keys in ascending order.
Now let us go through the reasoning behind the key function from the problem using an example (without using lambda expressions). If you sort the list just using sorted(lst)
, it will be sorted in ascending order and thus positive numbers will come after negative numbers. In order to ensure the first condition, i.e. that negative numbers come after positive numbers, we can instead sort the numbers with the keys being their negation. So we can for example define the key function
def sort_key_1(x):
return -x
If we assume that lst = [4, -2, 10, 5, -1, 8, -20]
, then sorted(lst, key=sort_key_1)
can be visualised as follows. We apply the key function to every element of the list:
Original list: 4, -2, 10, 5, -1, 8, -20
Keys: -4, 2, -10, -5, 1, -8, 20
Sorting the list of keys will yield the following:
-10, -8, -5, -4, 1, 2, 20
And if we take the original elements in this order, the result of the call sorted(lst, key=sort_key_1)
will be:
10, 8, 5, 4, -1, -2, -20
Now we still need to ensure that all elements are in fact sorted in ascending order. Since f(x) = -x
is a decreasing function it reverses the order of the elements, and both the positive and negative elements are now in descending order. We therefore need to change the order of the elements without changing the sign. One example of a function which is decreasing, but does not change the sign of its argument, is g(x) = 1/x
.
We can then combine this function with our original negation function to obtain the following key function:
def sort_key_2a(x):
return -1/x
However, we need to handle the case where x = 0
separately to avoid division by zero, so this leads us to the given key function, which will ensure that negative numbers come after positive numbers and both positive and negative numbers are sorted in ascending order.
def sort_key_2b(x):
return 0 if x == 0 else -1/x
The call sorted(lst, sort_key_2b))
can then be visualised as follows:
Original list: 4, -2, 10, 5, -1, 8, -20
Keys: -0.25, 0.5, -0.1, -0.2, 1, -0.125, 0.05
Sorting based on the keys yields:
-0.25, -0.2, -0.125, -0.1, 0.05, 0.5, 1
And thus the sorting result will be:
4, 5, 8, 10, -20, -2, -1
本文标签: pythonhow does lambdafunction work when sorting with conditionsStack Overflow
版权声明:本文标题:python - how does lambda-function work when sorting with conditions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741708725a2393714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
lambda <args>: <expression>
is equivalent todef f(<args>): return <expression>
, i.e. your sort key could be defined like this:def sort_key(x): return 0 if x == 0 else -1/x
– juanpa.arrivillaga Commented Feb 10 at 16:07lamda
functions work or about howsorted()
works? – JonSG Commented Feb 10 at 17:32