Create list of single item repeated N times

前端 未结 6 1213
南笙
南笙 2020-11-22 03:57

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = len

6条回答
  •  野的像风
    2020-11-22 04:36

    >>> [5] * 4
    [5, 5, 5, 5]
    

    Be careful when the item being repeated is a list. The list will not be cloned: all the elements will refer to the same list!

    >>> x=[5]
    >>> y=[x] * 4
    >>> y
    [[5], [5], [5], [5]]
    >>> y[0][0] = 6
    >>> y
    [[6], [6], [6], [6]]
    

提交回复
热议问题