Asterisk in function call

前端 未结 3 2067
渐次进展
渐次进展 2020-11-22 17:28

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

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

how is

相关标签:
3条回答
  • 2020-11-22 17:43

    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)
    
    0 讨论(0)
  • 2020-11-22 17:46

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

    So if uniqueCrossTabs was [ [ 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.

    As Jochen Ritzel has pointed out in the comments, 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))
    
    0 讨论(0)
  • 2020-11-22 17:49

    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]
    
    0 讨论(0)
提交回复
热议问题