Repeat each item in a list a number of times specified in another list

后端 未结 4 1406
情歌与酒
情歌与酒 2020-12-03 23:59

I have two lists, x and y:

>>> x = [2, 3, 4]
>>> y = [1, 2, 3]

I want to use these to create a

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 00:22

    Simple using for loop.

    >>> x = [2, 3, 4]
    >>> y = [1, 2, 3]
    >>> final = []
    >>> for index, item in enumerate(y):
            final.extend([x[index]]*item)
    

提交回复
热议问题