How to replicate array to specific length array

前端 未结 4 1153
半阙折子戏
半阙折子戏 2020-12-03 12:39

I want replicate a small array to specific length array

Example:

var = [22,33,44,55] # ==> len(var) = 4
n = 13

The new array tha

4条回答
  •  清歌不尽
    2020-12-03 13:09

    Copy the array (called lists in python) with [:] because they are mutable. The python short cut is then just to multiply the copy and add one more element.

    >>> var = [22, 33, 44, 55]
    >>> n = 3
    >>> newlist = var[:]*n + var[:1]
    

    gives the 13 elements you want.

提交回复
热议问题