Код: Выделить всё
overlay
Код: Выделить всё
arr
Код: Выделить всё
overlay = [ 0 , 1 , 1 , 4 , 3]
subixs = [[0, 1, 2, 3], [4, 5], [6], [7], 8] # relationship to `arr`, i.e. overlay[0] sets `arr[0]`, `arr[1]`, `arr[2]` and `arr[3]`
assert len(overlay) == len(subixs)
arr = [0, 0, 0, 0, 1, 1, 1, 4, 3]
< /code>
overlay[1] = 7
Например, представьте себе: < /p>
Код: Выделить всё
msk = (overlay == 1) | (overlay == 3)
overlay[msk] = [44, 48, 47]
< /code>
in that case msk
Код: Выделить всё
[44, 48, 47]
Я выяснил, как сделать перевод в индексы в ARR , но это довольно неэффективно, поскольку он использует списки, и я также не выяснил, как расширить значения.
Код: Выделить всё
new_value = [44, 48, 47]
msk = (overlay == 1) | (overlay == 3)
overlay[msk] = new_value
# convert to indices in `arr`
# Keep as list as numpy requires symmetric arrays
subixs = [[0, 1, 2, 3], [4, 5], [6], [7], 8]
subixs = np.asarray(subixs, dtype=object)
arr_ixs = sum(subixs[msk], [])
new_arr_value = ? # should be `[44, 44, 48, 47]`
arr[arr_ixs] = new_arr_value
< /code>
How would I go about making the index translation more efficient (using numpy's vectorised functions) and expand new_value
Код: Выделить всё
value_subixs = sub_ixs[msk]
arr_ixs = sum(value_subixs, [])
new_arr_value = np.repeat(value, np.asarray([len(ix) for ix in value_subixs]))
arr[arr_ixs] = new_arr_value
< /code>
However, both solution (index translation and value expansion) are using lists which is not desirable as arr
Подробнее здесь: https://stackoverflow.com/questions/796 ... ther-array