不使用 *(星号)打开包装

发布于 2025-01-20 11:14:30 字数 305 浏览 2 评论 0原文

如果我想保持工作功能,并且不想在中间使用*,那么等效替代函数是什么? 例如,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

德意的啸 2025-01-27 11:14:30

funct> funct> functools.duce 可以是用于将两个参数的函数应用于一个累积的估计,这对您的示例情况有效

import operator as op
from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A'])))

functools.reduce can be used to apply a function of two arguments to an iterable cumulatively, which would work for your example case

import operator as op
from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A'])))
爱*していゐ 2025-01-27 11:14:30

您可以使用 降低 检查所有映射元素是否彼此相等。

from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A'])))

不过,这并不是一件明智的事情。我之所以这么说是因为redable上面的调用不会概括为元素或更少元素的案例:

  • 如果输入为空,则会在typeerror:redy> typeerror:redial(redy)的空序列中失败,而没有初始值
  • 如果给出了一个单项输入列表,它将返回字符串而不是布尔<代码> true 或false
  • 如果给出带有3个以上项目的列表,它最终将将字符串与布尔值进行比较,并始终导致false

如果您的计划要用两个操作数,请致电op.eq,不再和少于两个操作数,则可以使用*拆开包装是合适的。即使在功能上下文中。

You could use reduce to check if all of the mapped elements are equal to each other.

from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A'])))

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:

  • If the input were empty it would fail with TypeError: reduce() of empty sequence with no initial value.
  • If it were given a one-item input list it would return a string rather than a boolean True or False.
  • If it were given list with 3+ items it would end up comparing strings to booleans and would always result in 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.

猫九 2025-01-27 11:14:30

实现此目标的一种方法是创建一个名为应用的函数。

def apply(func, args):
    return func(*args)
apply(op.eq, map(str.upper, ['a', 'A']))

One way to achieve this is by creating a function named apply.

def apply(func, args):
    return func(*args)
apply(op.eq, map(str.upper, ['a', 'A']))
空心↖ 2025-01-27 11:14:30

您可以使用降低解决此问题,如果您知道不会发生某种安全价值。例如,如果您知道将不在您的列表中,则可以做

import functools
mylist = list(map(str.upper, ['a', 'A']))
result = functools.reduce(lambda x, y: x if x == y else None, mylist)
print('all equal' if result != None else 'some differ')

You can solve this with reduce if there is some safe value you know will not occur. For example, if you know None will not be in your list, you can do

import functools
mylist = list(map(str.upper, ['a', 'A']))
result = functools.reduce(lambda x, y: x if x == y else None, mylist)
print('all equal' if result != None else 'some differ')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文