I had tried to return values to a and b by using the below method
(lambda a,b:print(a,b))((lambda x:(x,[int(i)**len(x) for i in x]))('153'))
but this shows error,i need some help to fix this.
TypeError: <lambda>() missing 1 required positional argument: 'b'
The inner function returns a single tuple of two values, but the outer function expects two separate values. Use *
-unpacking to have each value of the tuple passed as a separate parameter:
# v takes two parameters v provides one tuple of two values
(lambda a,b:print(a,b))(*(lambda x:(x,[int(i)**len(x) for i in x]))('153'))
# ^ unpack operator
Note that print
already takes positional arguments – (lambda a,b:print(a,b))
can be replaced by just print
. Also, Python3.8 introduces the :=
assignment operator, which can often be used instead of a lambda
to emulate let
expressions. This shortens the expression significantly:
# v print takes multiple arguments
print(*(x := '153', [int(i)**len(x) for i in x]))
# ^ assignment operator binds in current scope
@MisterMiyagi posted the correct answer using the given structure. However, I can't think of a case where using two lambdas in the way you did would be useful. Defining a function would make the code much more readable:
def print_values(string):
values = [int(i)**len(string) for i in string]
print(string, values)
print_values("153")
Or if you want it shorter:
def print_values(string):
print(string, [int(i)**len(string) for i in string])
print_values("153")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.