问题描述
尝试基于numpy
apply_along_axis
内置函数row index position
import numpy as np
sa = np.array(np.arange(4))
sa_changed = (np.repeat(sa.reshape(1,len(sa)),repeats=2,axis=0))
print (sa_changed)
操作:
[[0 1 2 3]
[0 1 2 3]]
功能:
np.apply_along_axis(lambda x: x+10,sa_changed)
操作:
array([[10,11,12,13],[10,13]])
但是有没有办法基于 row index position
使用这个函数,例如,如果它是一个 even row index
那么 add 10
如果它是一个 odd row index
那么 {{1} }
示例:
add 50
解决方法
直接或使用 apply_along_axis
迭代数组时,子数组没有 .index
属性。因此,我们必须将显式索引值传递给您的函数:
In [248]: def func(i,x):
...: if i//2==0:
...: x = x+10
...: else:
...: x = x+50
...: return x
...:
In [249]: arr = np.arange(10).reshape(5,2)
apply
没有办法添加这个索引,所以我们必须使用显式迭代。
In [250]: np.array([func(i,v) for i,v in enumerate(arr)])
Out[250]:
array([[10,11],[12,13],[54,55],[56,57],[58,59]])
用%替换//
In [251]: def func(i,x):
...: if i%2==0:
...: x = x+10
...: else:
...: x = x+50
...: return x
...:
In [252]: np.array([func(i,v in enumerate(arr)])
Out[252]:
array([[10,[52,53],[14,15],[18,19]])
但更好的方法是完全跳过迭代:
制作一个行添加数组:
In [253]: np.where(np.arange(5)%2,10,50)
Out[253]: array([50,50,50])
通过 broadcasting
应用:
In [256]: x+np.where(np.arange(5)%2,10)[:,None]
Out[256]:
array([[10,19]])
,
这是一种方法
let users=require('./user');
let products=require('./product');
export function getOrders(req,res) {
return order.findAndCountAll({
include: [
{model: users,required: true},// true for INNER JOIN
{model: products,required: false} // false for LEFT OUTER JOIN
],them all
})
}
偶数行索引
import numpy as np
x = np.array([[0,1,2,3],[0,3]])
y = x.copy() # if you dont wish to modify x
对于奇数行索引
y[::2] = y[::2] + 10
输出:
y[1::2] = y[1::2] + 50