📜  Python|通过将每个元素填充 N 来增加列表大小

📅  最后修改于: 2022-05-13 01:54:42.768000             🧑  作者: Mango

Python|通过将每个元素填充 N 来增加列表大小

给定一个列表和一个整数 N,编写一个Python程序,通过将每个元素填充 N 来增加列表的大小。

例子:

Input : lst = [1, 2, 3]
        N = 3
Output : [1, 1, 1, 2, 2, 2, 3, 3, 3]

Input : lst = ['cats', 'dogs']
        N = 2
Output : ['cats', 'cats', 'dogs', 'dogs']

方法#1:列表理解

# Python3 program to increase list size 
# by padding each element by N
  
def increaseSize(lst, N):
      
    return [el for el in lst for _ in range(N)]
          
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
输出:
[1, 1, 1, 2, 2, 2, 3, 3, 3]


方法 #2:使用functools.reduce()方法

reduce函数将在其参数中传递的特定函数应用于所有列表元素。因此,在这种方法中,我们在每个元素上应用一个函数,它的出现乘以 N。

# Python3 program to increase list size 
# by padding each element by N
from functools import reduce
  
def increaseSize(lst, N):
      
    return reduce(lambda x, y: x + y, [[el]*N for el in lst])
          
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
输出:
[1, 1, 1, 2, 2, 2, 3, 3, 3]


方法 #3:使用itertools.chain()

# Python3 program to increase list size 
# by padding each element by N
from itertools import chain
  
def increaseSize(lst, N):
      
    return list(chain(*([el]*N for el in lst)))
          
# Driver code
lst = [1, 2, 3]
N = 3
print(increaseSize(lst, N))
输出:
[1, 1, 1, 2, 2, 2, 3, 3, 3]