However, IMO, this is better than the hack of using using multiple expressions in a tuple and returning the last one. This is just to suggest that it can be done, and should not be taken as a recommendation. Print(min(points, key=lambda y: next(b for (a,b),c in ))) Print(min(points, key=lambda p: (lambda a,b: (lambda x,y: (y))(*a))(*p)))Īlternatively one can also use a list instead of a tuple. In comparison to the accepted answer using an unwrapper lambda: values = Print(min(values, key=lambda y: next(b for ((a,b),c) in (y,)))) In comparison with the accepted answer of using a wrapper, this solution is able to completely destructure the arguments while the wrapper only destructures the first level. Print(min(points, key=lambda y: next(x*x + y*y for (x,y) in ))) It is possible to abuse it to obtain similar behavior in Python 3. Let us discuss both ways for tuple unpacking in Python. We use the Python unpacking operator and parallel assignment to unpack collection objects. While the destructuring arguments was removed in Python3, it was not removed from comprehensions. The operator is an unpacking operator that will unpack the values from any iterable object, such as lists, tuples, strings, etc For example, if we want to unpack numlist and pass in the 5 elements as separate arguments for the numsum function, we could do so as follows: numsum(numlist) 15. In Python, the unpacking operation is used to assign elements of a collection object like a list, tuple, dictionary, etc directly to different variables. Still, there are possible uses - if there are various one-liners that would operate on this point, it could be worth to have a namedtuple, and use the assignment expression to effectively "cast" the incoming sequence to the namedtuple: > from collections import namedtuple I can unpack it with: > for letter, word in seqnested: print letter, word A Apple B Boat C Cat How should I unpack it to get the following 0 A Apple 1 B Boat 2 C Cat The only way I know is to use a counter/incrementor, which is un-Pythonic as far as I know. One should keep in mind that this kind of code will seldom be more readable or practical than having a full function. So, if one uses a trick to execute multiple expressions inside a lambda - I usually do that by creating a tuple and just returning the last component of it, it is possible to do the following: > a = lambda p:(x:=p, y:=p, x ** 2 + y ** 2) Tuple unpacking is most often used, not with an equals sign, but instead in a for loop. Since the release of Python 3.8, PEP 572 - assignment expressions - have been available as a tool. The way to go would be to raise this issue on the Python ideas mailing list, but be prepared to argue a lot over there to gain some traction.Īctually, just not to say "there is no way out", a third way could be to implement one more level of lambda calling just to unfold the parameters - but that would be at once more inefficient and harder to read than your two suggestions: min(points, key=lambda p: (lambda x,y: (x*x + y*y))(*p))
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |