![](/img/trans.png)
[英]How do I generate a new array that each element is the the maximum number within a local range of a numpy 1d array
[英]Numpy: Create new array from 1d integer array where each element in integer array represents the number of elements in the new array
這看起來應該很簡單,但我很難過(對 numpy 來說也很新。)
我有一個一維整數數組a 。
我想生成一個新的一維數組b使得:
這是一口,所以這里有一個我想讓它更具體的例子:
a = array([2,3,3,4])
CONSTANT = 120
.
.
.
b = array([60,60,
40,40,40,
40,40,40,
30,30,30,30])
任何幫助表示贊賞!
我認為一個非常明確的方法是
import numpy as np
a = np.array([2,3,3,4])
constant = 120
#numpy.repeat(x,t) repeats the val x t times you can use x and t as vectors of same len
b = np.repeat(constant/a , a)
你可以用np.concatenate
和老式的zip
來做到這一點:
>>> elements = CONSTANT / a
>>> np.concatenate([np.array([e]*n) for e, n in zip(elements, a)])
array([60., 60., 40., 40., 40., 40., 40., 40., 30., 30., 30., 30.])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.