本文介绍了重复数组中不同的元素不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个经度数组lonPorts
Say I have an array with longitudes, lonPorts
lonPort =np.loadtxt('LongPorts.txt',delimiter=',')例如:
lonPort=[0,1,2,3,...]我想重复每个元素不同的时间.我该怎么做呢?这是我尝试过的:
And I want to repeat each element a different amount of times. How do I do this? This is what I tried:
Repeat =[5, 3, 2, 3,...] lonPort1=[] for i in range (0,len(lenDates)): lonPort1[sum(Repeat[0:i])]=np.tile(lonPort[i],Repeat[i])因此结果将是:
lonPort1=[0,0,0,0,0,1,1,1,2,2,3,3,3,...]我得到的错误是:
list assignment index out of range如何消除错误并制作数组? 谢谢!
How do I get rid of the error and make my array? Thank you!
推荐答案您可以使用 np.repeat() :
You can use np.repeat():
np.repeat(a, [5,3,2,3])示例:
In [3]: a = np.array([0,1,2,3]) In [4]: np.repeat(a, [5,3,2,3]) Out[4]: array([0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3])更多推荐
重复数组中不同的元素不同的时间
发布评论