重复数组中不同的元素不同的时间

编程入门 行业动态 更新时间:2024-10-24 04:48:29
本文介绍了重复数组中不同的元素不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说我有一个经度数组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])

更多推荐

重复数组中不同的元素不同的时间

本文发布于:2023-07-18 04:36:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   元素   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!