Can someone explain this statement? lpadded = win // 2 * [-1] + l + win // 2 * [-1]

前端 未结 1 403
清歌不尽
清歌不尽 2021-01-21 20:49

Given that l is a list of integers and win is an integer, the following code produces a list lpadded:

lpadded = win // 2 * [-1] + l + win // 2 * [-1]


        
相关标签:
1条回答
  • 2021-01-21 21:05

    In Python, you can “multiply” a list by an integer to build a list of repeated elements.

    >>> [42] * 4
    [42, 42, 42, 42]
    >>> ['hello', 'world'] * 3
    ['hello', 'world', 'hello', 'world', 'hello', 'world']
    

    So the expression win // 2 * [-1] creates a list object containing win // 2 copies of the number -1.

    0 讨论(0)
提交回复
热议问题