如何重复列表n
次的每个元素并形成新列表?例如:
x=[1,2,3,4]
n=3
x1=[1,1,1,2,2,2,3,3,3,4,4,4]
x*n
无法正常工作
for i in x[i]
x1=n*x[i]
必须有一种简单而聪明的方式。
答案 0 :(得分:70)
理想的方法可能是numpy.repeat
:
In [16]:
x1=[1,2,3,4]
In [17]:
np.repeat(x1,3)
Out[17]:
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
答案 1 :(得分:26)
如果你真的想要结果作为列表,并且生成器是不够的:
import itertools
lst = range(1,5)
list(itertools.chain.from_iterable(itertools.repeat(x, 3) for x in lst))
Out[8]: [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
答案 2 :(得分:24)
您可以使用列表理解:
[item for item in x for i in range(n)]
>>> x=[1, 2, 3, 4]
>>> n = 3
>>> new = [item for item in x for i in range(n)]
>>> new
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
>>>
答案 3 :(得分:6)
嵌套的list-comp在这里工作:
>>> [i for i in range(10) for _ in xrange(3)]
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9]
或者使用你的例子:
>>> x = [1, 2, 3, 4]
>>> n = 3
>>> [i for i in x for _ in xrange(n)]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
答案 4 :(得分:2)
[myList[i//n] for i in range(n*len(myList))]
答案 5 :(得分:1)
实现此目的的更简单方法是将列表x
与n
相乘并对结果列表进行排序。 e.g。
>>> x = [1,2,3,4]
>>> n = 3
>>> a = sorted(x*n)
>>> a
>>> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
答案 6 :(得分:1)
这将解决您的问题:
x=[1,2,3,4]
n = 3
x = sorted(x * n)
答案 7 :(得分:0)
import itertools
def expand(lst, n):
lst = [[i]*n for i in lst]
lst = list(itertools.chain.from_iterable(lst))
return lst
x=[1,2,3,4]
n=3
x1 = expand(x,3)
print(x1)
给出:
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
说明:
执行,[3]*3
给出[3,3,3]
的结果,将其替换为n
我们得到[3,3,3,...3] (n times)
使用列表理解我们可以遍历列表的每个元素并执行这个操作,最后我们需要展平列表,我们可以通过list(itertools.chain.from_iterable(lst))
答案 8 :(得分:0)
如果您想要就地修改列表,最好的方法是从后面进行迭代,并将之前一个项目的切片分配给该项目的列表n
次。
这是因为切片分配:
>>> ls = [1, 2, 3]
>>> ls[0: 0+1]
[1]
>>> ls[0: 0+1] = [4, 5, 6]
>>> ls
>>> [4, 5, 6, 2, 3]
def repeat_elements(ls, times):
for i in range(len(ls) - 1, -1, -1):
ls[i: i+1] = [ls[i]] * times
演示用法:
>>> a = [1, 2, 3]
>>> b = a
>>> b
[1, 2, 3]
>>> repeat_elements(b, 3)
>>> b
[1, 1, 1, 2, 2, 2, 3, 3, 3]
>>> a
[1, 1, 1, 2, 2, 2, 3, 3, 3]
(如果你不想就地修改它,你可以复制列表并返回副本,这不会修改原文。这也适用于其他序列,如tuple
s ,但不像itertools.chain.from_iterable
和itertools.repeat
方法那样懒惰)
def repeat_elements(ls, times):
ls = list(ls) # Makes a copy
for i in range(len(ls) - 1, -1, -1):
ls[i: i+1] = [ls[i]] * times
return ls
答案 9 :(得分:0)
zAxe=[]
for i in range(5):
zAxe0 =[i] * 3
zAxe +=(zAxe0) # append allows accimulation of data
答案 10 :(得分:0)
方式1:
def foo():
for j in [1, 3, 2]:
yield from [j]*5
方式2:
from itertools import chain
l= [3, 1, 2]
chain(*zip(*[l]*3))
方式3:
sum(([i]*5 for i in [2, 1, 3]), [])
答案 11 :(得分:-1)
对于基本Python 2.7:
from itertools import repeat
def expandGrid(**kwargs):
# Input is a series of lists as named arguments
# output is a dictionary defining each combination, preserving names
#
# lengths of each input list
listLens = [len(e) for e in kwargs.itervalues()]
# multiply all list lengths together to get total number of combinations
nCombos = reduce((lambda x, y: x * y), listLens)
iDict = {}
nTimesRepEachValue=1 #initialize as repeating only once
for key in kwargs.keys():
nTimesRepList=nCombos/(len(kwargs[key])*nTimesRepEachValue)
tempVals=[] #temporary list to store repeated
for v in range(nTimesRepList):
indicesToAdd=reduce((lambda x,y: list(x)+list(y)),[repeat(x, nTimesRepEachValue) for x in kwargs[key]])
tempVals=tempVals+indicesToAdd
iDict[key] = tempVals
# Accumulating the number of times needed to repeat each value
nTimesRepEachValue=len(kwargs[key])*nTimesRepEachValue
return iDict
#Example usage:
expandedDict=expandGrid(letters=["a","b","c","d"],nums=[1,2,3],both=["v",3])