函数调用中的星号

发布于 2024-10-21 12:25:33 字数 228 浏览 2 评论 0原文

我正在使用 itertools.chain 以这种方式“展平”列表列表:

uniqueCrossTabs = list(itertools.chain(*uniqueCrossTabs))

这与说有什么不同:

uniqueCrossTabs = list(itertools.chain(uniqueCrossTabs))

I'm using itertools.chain to "flatten" a list of lists in this fashion:

uniqueCrossTabs = list(itertools.chain(*uniqueCrossTabs))

how is this different than saying:

uniqueCrossTabs = list(itertools.chain(uniqueCrossTabs))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

高跟鞋的旋律 2024-10-28 12:25:33

* 是“splat”运算符:它将像列表一样的可迭代对象作为输入,并将其扩展为函数调用中的实际位置参数。

因此,如果 uniqueCrossTabs[[1, 2], [3, 4]],则 itertools.chain(*uniqueCrossTabs) 是相同的正如所说的 itertools.chain([1, 2], [3, 4])

这显然与仅传入 uniqueCrossTabs 不同。就您而言,您有一个想要展平的列表列表; itertools.chain() 的作用是在传递给它的所有位置参数的串联上返回一个迭代器,其中每个位置参数本身都是可迭代的。

换句话说,您希望将 uniqueCrossTabs 中的每个列表作为参数传递给 chain(),这会将它们链接在一起,但您没有单独的列表变量,因此您可以使用 * 运算符将列表列表展开为多个列表参数。

chain.from_iterable() 是更适合此操作,因为它一开始就假设有一个可迭代的可迭代。然后你的代码就变得简单了:

uniqueCrossTabs = list(itertools.chain.from_iterable(uniqueCrossTabs))

* is the "splat" operator: It takes an iterable like a list as input, and expands it into actual positional arguments in the function call.

So if uniqueCrossTabs were [[1, 2], [3, 4]], then itertools.chain(*uniqueCrossTabs) is the same as saying itertools.chain([1, 2], [3, 4])

This is obviously different from passing in just uniqueCrossTabs. In your case, you have a list of lists that you wish to flatten; what itertools.chain() does is return an iterator over the concatenation of all the positional arguments you pass to it, where each positional argument is iterable in its own right.

In other words, you want to pass each list in uniqueCrossTabs as an argument to chain(), which will chain them together, but you don't have the lists in separate variables, so you use the * operator to expand the list of lists into several list arguments.

chain.from_iterable() is better-suited for this operation, as it assumes a single iterable of iterables to begin with. Your code then becomes simply:

uniqueCrossTabs = list(itertools.chain.from_iterable(uniqueCrossTabs))
葬花如无物 2024-10-28 12:25:33

它将序列拆分为函数调用的单独参数。

>>> def foo(a, b=None, c=None):
...   print a, b, c
... 
>>> foo([1, 2, 3])
[1, 2, 3] None None
>>> foo(*[1, 2, 3])
1 2 3
>>> def bar(*a):
...   print a
... 
>>> bar([1, 2, 3])
([1, 2, 3],)
>>> bar(*[1, 2, 3])
(1, 2, 3)

It splits the sequence into separate arguments for the function call.

>>> def foo(a, b=None, c=None):
...   print a, b, c
... 
>>> foo([1, 2, 3])
[1, 2, 3] None None
>>> foo(*[1, 2, 3])
1 2 3
>>> def bar(*a):
...   print a
... 
>>> bar([1, 2, 3])
([1, 2, 3],)
>>> bar(*[1, 2, 3])
(1, 2, 3)
颜漓半夏 2024-10-28 12:25:33

只是解释概念/使用它的另一种方式。

import random

def arbitrary():
    return [x for x in range(1, random.randint(3,10))]

a, b, *rest = arbitrary()

# a = 1
# b = 2
# rest = [3,4,5]

Just an alternative way of explaining the concept/using it.

import random

def arbitrary():
    return [x for x in range(1, random.randint(3,10))]

a, b, *rest = arbitrary()

# a = 1
# b = 2
# rest = [3,4,5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文