Defining a list in Python using the multiply operator

前端 未结 2 1751
迷失自我
迷失自我 2021-01-19 06:31

Recently in Python I have encountered this statement:

board.append([\' \'] * 8)

I have tried to search the Internet with Google to find som

2条回答
  •  无人共我
    2021-01-19 07:01

    Can you please refer me to a place where I can find some more information about this type of statements.

    Most of the relevant operators and methods are defined here: Sequence Types.

    Specifically s * n is defined as

    s * n, n * s -- n shallow copies of s concatenated

    Here, s is a sequence and n is a number.

    Thus, [' '] * 8 returns a list consisting of eight ' '.

    board.append() appends the result to board, which presumably is a list (of lists).

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题