不使用 *(星号)打开包装
如果我想保持工作功能,并且不想在中间使用*
,那么等效替代函数是什么? 例如,
import operator as op
print(op.eq(*map(str.upper, ['a', 'A'])))
如何在此处避免使用*
?
我创建了一个函数,
def unpack(args):
return *args
但是它给出了语法错误,print(*args)
workt
If I want to keep things functional, and do not want to use *
in the middle, then what is the equivalent substitute function?
for example,
import operator as op
print(op.eq(*map(str.upper, ['a', 'A'])))
how do I avoid using *
here?
I created a function, like,
def unpack(args):
return *args
but it gives syntax error, print(*args)
works but return
fails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
funct> funct> functools.duce
可以是用于将两个参数的函数应用于一个累积的估计,这对您的示例情况有效functools.reduce
can be used to apply a function of two arguments to an iterable cumulatively, which would work for your example case您可以使用
降低 检查所有映射元素是否彼此相等。
不过,这并不是一件明智的事情。我之所以这么说是因为
redable
上面的调用不会概括为元素或更少元素的案例:typeerror:redy> typeerror:redial(redy)的空序列中失败,而没有初始值
。false
。false
。如果您的计划要用两个操作数,请致电
op.eq
,不再和少于两个操作数,则可以使用*
拆开包装是合适的。即使在功能上下文中。You could use
reduce
to check if all of the mapped elements are equal to each other.This isn't really a sensible thing to do, though. I say this because the
reduce
call above doesn't generalize to cases with more or fewer elements:TypeError: reduce() of empty sequence with no initial value
.True
orFalse
.False
.If your plan is to call
op.eq
with exactly two operands, no more and no less, then unpacking with*
is appropriate. Even in a functional context.实现此目标的一种方法是创建一个名为
应用
的函数。One way to achieve this is by creating a function named
apply
.您可以使用
降低
解决此问题,如果您知道不会发生某种安全价值。例如,如果您知道无
将不在您的列表中,则可以做You can solve this with
reduce
if there is some safe value you know will not occur. For example, if you knowNone
will not be in your list, you can do