1

Sort Key Lambda Parameters

I do not understand how the lambda parameters are working, the [-e[0],e[1]] portion is especially confusing. I have removed all the excessive printing code and I have also removed all unnecessary code from my question. What does the parameter -e[0] achieve and what is that e[1] achieves?

data.sort(key = lambda e: [-e[0],e[1]]) # --> anonymous function
print ("This is the data sort after the lambda filter but NOT -e %s" %data)`

[in] 'aeeccccbbbbwwzzzwww'
[out] This is the data before the sort [[2, 'e'], [4, 'c'], [1, 'a'], [4, 'b'], [5, 'w'], [3, 'z']]
[out] This is the data sort before the lambda filter [[1, 'a'], [2, 'e'], [3, 'z'], [4, 'b'], [4, 'c'], [5, 'w']]
[out] This is the data sort after the lambda filter but NOT -e [[1, 'a'], [2, 'e'], [3, 'z'], [4, 'b'], [4, 'c'], [5, 'w']]
[out] This is the data sort after the lambda filter [[5, 'w'], [4, 'b'], [4, 'c'], [3, 'z'], [2, 'e'], [1, 'a']]

[out] w 5
[out] b 4
[out] c 4
5
  • -e[0] is -1 * (the_first_element_in_e), i.e. for e == [2, 'e'] it would be -2. What exactly is confusing you? Commented Aug 23, 2015 at 14:36
  • How would having -2 help us rank the key & the values? We are trying to create a program that spits out how many times a word occurred in descending order. Commented Aug 23, 2015 at 15:03
  • 1
    Multiplying by -1 means they'll sort highest first, instead of the usual lowest first. Remove the - and see what changes. Commented Aug 23, 2015 at 15:04
  • Oh it would just reverse the order of the sorting function, but not turn any values negative? Oh I see. Also, we had to create a lambda function in order to iterate through the list indices. Commented Aug 23, 2015 at 15:10
  • You're creating a new object to sort on, not changing the original objects. Is that a question? Commented Aug 23, 2015 at 15:11

1 Answer 1

1

l = [[2, 'e'], [4, 'c'], [1, 'a'], [4, 'b'], [5, 'w'], [3, 'z']]

>>> l.sort()

Normal sort: first the first element of the nested list is considered and then the second element.

>>>l.sort(key=lambda e: [e[0], e[1]])

Similar to l.sort()

>>>l.sort(key=lambda e: [-e[0], e[1]])

Now, what is does is- Reverse sort the the list on the basis of first element of the nested list AND sort normally on the internal elements of the nested sorted list i.e

first 2,3,4,5 etc are considered for sorting the list in reverse order( -e[0] == -2,-3,-4...) and then we sort the elements on the basis of second element for internal sorting (e[1] == 'w', 'a', 'b'...)

Sign up to request clarification or add additional context in comments.

4 Comments

That is the answer that I was looking for.
This gives the same answer... data.sort( key = lambda e: -e[0] ) So what's the point of data.sort( key = lambda e: [-e[0], e[1]] ) ? How is it different?
I am not sure if you got a notification for this or not. I will mark your answer as correct once you reply please, and thank you. @Death-Stalker
@Rafeh No, data.sort( key = lambda e: -e[0] ) ==> [[5, 'w'], [4, 'c'], [4, 'b'], [3, 'z'], [2, 'e'], [1, 'a']] and data.sort( key = lambda e: [-e[0], e[1]] ) ==> [[5, 'w'], [4, 'b'], [4, 'c'], [3, 'z'], [2, 'e'], [1, 'a'] . See the [4, 'c'], [4, 'b'] and [4, 'b'], [4, 'c'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.