【发布时间】:2017-03-01 20:21:21
【问题描述】:
我有一个 2D 数组(它实际上非常大并且是另一个数组的视图):
x = np.array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]]
)
我有一个处理数组每一行的函数:
def some_func(a):
"""
Some function that does something funky with a row of numbers
"""
return [a[2], a[0]] # This is not so funky
np.apply_along_axis(some_func, 1, x)
我正在寻找的是调用np.apply_along_axis 函数的某种方式,以便我可以访问行索引(对于正在处理的行),然后能够使用此函数处理每一行:
def some_func(a, idx):
"""
I plan to use the index for some logic on which columns to
return. This is only an example
"""
return [idx, a[2], a[0]] # This is not so funky
【问题讨论】:
-
用范围数组压缩它?
-
@Divakar 你能举个例子吗?您可以假设 2D 数组是一个视图并且非常大,因此副本不是解决方案。
-
嗨,这个问题你解决了吗?
-
@slaw 这仍然是一个非首发!?疯狂