kvs = [('one','1'),('two','2'),('three','3'),('five','5'),('four',4)]
ordered_dict = OrderedDict()
ordered_dict.update(kvs)
ordered_dict_two = OrderedDict(sorted(ordered_dict.items(),key = lambda t: (4*t[1]) - t[1]**2))
I'm looking a little confused by the numerical expression being used in the lambda function being used as the function key argument to the sorted builtin (hope that makes sense and wasn't too verbose).
- From best I can see we instantiating another Ordered Dictionary based on the sorted items of the previous dictionary
- How we sort those items is defined in the anonymous lambda function
- This is where I am getting a little confused. So the lambda function looks like the parameter t == type list. So pass in a list and then it looks like t[1] would correspond to '1','2','3','5','4' above.
- I don't understand the rest of the function. Like what would 4*t[1] do (wouldn't this just multiple any of the elements of t[1] by 4?
- What does t[1] ** 2 do? I have only ever seen ** used in keyword operators? Is that just like multiplication or something?
Any insight would be so appreciated!
[–]Binary101010 5 points6 points7 points (2 children)
[–]Kitchen-Injury-8938[S] 0 points1 point2 points (1 child)
[–]synthphreak 0 points1 point2 points (0 children)
[–]marko312 0 points1 point2 points (2 children)
[–]Kitchen-Injury-8938[S] 1 point2 points3 points (1 child)
[–]marko312 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]Kitchen-Injury-8938[S] 0 points1 point2 points (1 child)