简体   繁体   English

在 Python/Numpy 中一次分配多个数组索引

[英]Assigning multiple array indices at once in Python/Numpy

I'm looking to quickly (hopefully without a for loop) generate a Numpy array of the form:我希望快速(希望没有 for 循环)生成一个 Numpy 形式的数组:

array([a,a,a,a,0,0,0,0,0,b,b,b,0,0,0, c,c,0,0....])

Where a, b, c and other values are repeated at different points for different ranges.其中a、b、c等值在不同点重复不同范围。 I'm really thinking of something like this:我真的在想这样的事情:

import numpy as np
a = np.zeros(100)
a[0:3,9:11,15:16] = np.array([a,b,c])

Which obviously doesn't work.这显然行不通。 Any suggestions?有什么建议么?

Edit (jterrace answered the original question): The data is coming in the form of an N*M Numpy array.编辑(jterrace 回答了原始问题):数据以 N*M Numpy 数组的形式出现。 Each row is mostly zeros, occasionally interspersed by sequences of non-zero numbers.每行大多是零,偶尔穿插非零数字序列。 I want to replace all elements of each such sequence with the last value of the sequence.我想用序列的最后一个值替换每个这样的序列的所有元素。 I'll take any fast method to do this!我会采取任何快速的方法来做到这一点! Using where and diff a few times, we can get the start and stop indices of each run.使用 where 和 diff 几次,我们可以获得每次运行的开始和停止索引。

raw_data = array([.....][....])
starts = array([0,0,0,1,1,1,1...][3, 9, 32, 7, 22, 45, 57,....])
stops = array([0,0,0,1,1,1,1...][5, 12, 50, 10, 30, 51, 65,....])
last_values = raw_data[stops]
length_to_repeat = stops[1]-starts[1]

Note that starts[0] and stops[0] are the same information (which row the run is occurring on).请注意,starts[0] 和stops[0] 是相同的信息(运行发生在哪一行)。 At this point, since the only route I know of is what jterrace suggest, we'll need to go through some contortions to get similar start/stop positions for the zeros, then interleave the zero start/stop with the values start/stops, and interleave the number 0 with the last_values array.在这一点上,由于我知道的唯一路线是 jterrace 建议的,我们需要 go 通过一些扭曲来获得类似的零开始/停止位置,然后将零开始/停止与值开始/停止交错,并将数字 0 与 last_values 数组交错。 Then we loop over each row, doing something like:然后我们遍历每一行,执行如下操作:

for i in range(N)
    values_in_this_row = where(starts[0]==i)[0]
    output[i] = numpy.repeat(last_values[values_in_this_row], length_to_repeat[values_in_this_row])

Does that make sense, or should I explain some more?这有意义吗,还是我应该再解释一下?

If you have the values and repeat counts fully specified, you can do it this way:如果您完全指定了值和重复计数,则可以这样做:

>>> import numpy
>>> values = numpy.array([1,0,2,0,3,0])
>>> counts = numpy.array([4,5,3,3,2,2])
>>> numpy.repeat(values, counts)
array([1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 3, 3, 0, 0])

you can use numpy.r_ :您可以使用numpy.r_

>>> np.r_[[a]*4,[b]*3,[c]*2]
array([1, 1, 1, 1, 2, 2, 2, 3, 3])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM