What is lambda? How can you explain this sorting [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Syntax behind sorted(key=lambda: …)
6 answers
Why are Python lambdas useful? [closed]
26 answers
Please be gentle I'm still learning Python. I've an example where
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
The result is
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Can you please help explain why this is the sequence?
python lambda
marked as duplicate by Idlehands, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Syntax behind sorted(key=lambda: …)
6 answers
Why are Python lambdas useful? [closed]
26 answers
Please be gentle I'm still learning Python. I've an example where
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
The result is
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Can you please help explain why this is the sequence?
python lambda
marked as duplicate by Idlehands, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Alambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions
– UnholySheep
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e.def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...
– Idlehands
Jan 3 at 15:15
1
Give this post a look
– yatu
Jan 3 at 15:16
All functions are anonymous; thedef
statement simply binds the function to a name after it has been defined. Alambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.
– chepner
Jan 3 at 15:16
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18
|
show 2 more comments
This question already has an answer here:
Syntax behind sorted(key=lambda: …)
6 answers
Why are Python lambdas useful? [closed]
26 answers
Please be gentle I'm still learning Python. I've an example where
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
The result is
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Can you please help explain why this is the sequence?
python lambda
This question already has an answer here:
Syntax behind sorted(key=lambda: …)
6 answers
Why are Python lambdas useful? [closed]
26 answers
Please be gentle I'm still learning Python. I've an example where
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
The result is
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Can you please help explain why this is the sequence?
This question already has an answer here:
Syntax behind sorted(key=lambda: …)
6 answers
Why are Python lambdas useful? [closed]
26 answers
python lambda
python lambda
asked Jan 3 at 15:13
MilkyMilky
1
1
marked as duplicate by Idlehands, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Idlehands, coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Alambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions
– UnholySheep
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e.def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...
– Idlehands
Jan 3 at 15:15
1
Give this post a look
– yatu
Jan 3 at 15:16
All functions are anonymous; thedef
statement simply binds the function to a name after it has been defined. Alambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.
– chepner
Jan 3 at 15:16
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18
|
show 2 more comments
Alambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions
– UnholySheep
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e.def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...
– Idlehands
Jan 3 at 15:15
1
Give this post a look
– yatu
Jan 3 at 15:16
All functions are anonymous; thedef
statement simply binds the function to a name after it has been defined. Alambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.
– chepner
Jan 3 at 15:16
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18
A
lambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions– UnholySheep
Jan 3 at 15:15
A
lambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions– UnholySheep
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e. def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...– Idlehands
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e. def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...– Idlehands
Jan 3 at 15:15
1
1
Give this post a look
– yatu
Jan 3 at 15:16
Give this post a look
– yatu
Jan 3 at 15:16
All functions are anonymous; the
def
statement simply binds the function to a name after it has been defined. A lambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.– chepner
Jan 3 at 15:16
All functions are anonymous; the
def
statement simply binds the function to a name after it has been defined. A lambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.– chepner
Jan 3 at 15:16
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18
|
show 2 more comments
1 Answer
1
active
oldest
votes
A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def key_func(pair):
return pair[1]
pairs.sort(key=key_func)
In this case, the lambda function is telling sort
how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def key_func(pair):
return pair[1]
pairs.sort(key=key_func)
In this case, the lambda function is telling sort
how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).
add a comment |
A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def key_func(pair):
return pair[1]
pairs.sort(key=key_func)
In this case, the lambda function is telling sort
how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).
add a comment |
A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def key_func(pair):
return pair[1]
pairs.sort(key=key_func)
In this case, the lambda function is telling sort
how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).
A lambda is simply a short way of declaring a simple function. That code could just as easily have been written:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
def key_func(pair):
return pair[1]
pairs.sort(key=key_func)
In this case, the lambda function is telling sort
how it should turn a value in the array (in this case, a tuple which doesn't have an obvious sorting order) into a value that it knows how to sort (a string).
answered Jan 3 at 15:19
Harry CuttsHarry Cutts
1,098921
1,098921
add a comment |
add a comment |
A
lambda
is an anonymous function: docs.python.org/3/tutorial/controlflow.html#lambda-expressions– UnholySheep
Jan 3 at 15:15
lambda
is the same as an unnamed function, i.e.def f(pair): return pair[1]
. You will do well to brush up on the vast documentations and tutorials on beginner basics of Python before venturing further...– Idlehands
Jan 3 at 15:15
1
Give this post a look
– yatu
Jan 3 at 15:16
All functions are anonymous; the
def
statement simply binds the function to a name after it has been defined. Alambda
expression is an expression (rather than a statement) that can create a function as long as the body of the function is itself a single expression.– chepner
Jan 3 at 15:16
Adding to what was already said : you are establishing a criteria on how to sort. The lambda function returns the second element for each tuple. Therefore, you are sorting by the second element of your tuples.
– Matina G
Jan 3 at 15:18