简体   繁体   English

我想从一个 lambda 返回两个值并分配给另一个,但我得到了错误

[英]i want to return two values from one lambda and assign to the other one but i got error

I had tried to return values to a and b by using the below method我曾尝试使用以下方法将值返回给 a 和 b

(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.内部 function 返回两个值的单个元组,但外部 function 需要两个单独的值。 Use * -unpacking to have each value of the tuple passed as a separate parameter:使用* -unpacking将元组的每个值作为单独的参数传递:

#       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 .请注意, print已经占据了位置 arguments - (lambda a,b:print(a,b))可以仅替换为print Also, Python3.8 introduces the := assignment operator, which can often be used instead of a lambda to emulate let expressions.此外,Python3.8 引入了:=赋值运算符,它通常可以用来代替lambda来模拟let表达式。 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. @MisterMiyagi 使用给定的结构发布了正确答案。 However, I can't think of a case where using two lambdas in the way you did would be useful.但是,我想不出以您的方式使用两个 lambda 表达式会有用的情况。 Defining a function would make the code much more readable:定义 function 将使代码更具可读性:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将一个分配给另一个时,为什么这两个值不同? - Why are these two values different when I assign one to the other? 我想返回两个值,但只打印一个 - i want to return two values, but only print one 我想将一个df列附加在另一个列之下,并根据其他列分配行值 - I want to attach one df column below another and assign row values based on other columns 我有两个列表,一个用于x值,一个用于y值,我想从这些列表中创建一个列表 - I have two lists one for x values and one for y values, I want to make a list from these 我有两个列表,我想从中创建一个字典。 但是我想为一个键存储多个值 - I have two list and I want to create a dictionary from it . But I want to store multiple values for one key 我有两个代码脚本,我只想将一个特定的行从一个导入到另一个。 我该怎么做? - I have two scripts of code and i want to import just one specific line from one into the other. How do i do it? 我想迭代两个循环,一个是列表,另一个是 python 中的语句 - I want to iterate two loops one is list and the other is a statement in python 如果站点有两个链接,一个是我想要的,另一个是我不想要的,如何从站点中提取特定链接? - How to extract a particular link from a site if it has two links one which I want and other which I don't want? 我想要一个反斜线-而不是两个 - I want one backslash - not two 我有 2 个 Pandas 数据帧,想从一个数据帧中提取值来填充另一个数据帧的列? - I have 2 pandas dataframes and want to pull values from one dataframe to fill in columns of the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM