【问题标题】:Create new array with array elements/amounts set by two other arrays in Python使用 Python 中的其他两个数组设置的数组元素/数量创建新数组
【发布时间】:2012-02-10 19:00:11
【问题描述】:

我在 Python 中有两个数组(numpy 数组):

a=array([5,7,3,5])
b=array([1,2,3,4])

我希望创建第三个数组,其中 b 中的每个元素在新数组中出现 a 次,如下所示:

c=array([1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,4,4,4,4,4])

有没有一种快速的、numPythonic 的方式来做到这一点,并且只需要最少的循环?我需要在一个相当大的数组上循环使用这个操作数千次,所以我希望它尽可能快。

干杯, 迈克

【问题讨论】:

    标签: python arrays numpy scipy


    【解决方案1】:

    我相信repeat 是你想要的:

    c = repeat(b, a)
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 2021-10-16
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      相关资源
      最近更新 更多