混合列表推导,np.linspace
它非常简单:
>>> a = [1, 1.1, 1.2, 1.3, 1.4, 1.5]
>>> b = [np.linspace(0, j, 11) for j in a]
>>> b
[array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
array([ 0. , 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88, 0.99,
1.1 ]),
...
array([ 0. , 0.15, 0.3 , 0.45, 0.6 , 0.75, 0.9 , 1.05, 1.2 , 1.35,
1.5 ])]
>>> c = [np.linspace(j, 2, 6) for j in a]
>>> c
[array([ 1. , 1.2, 1.4, 1.6, 1.8, 2. ]),
array([ 1.1 , 1.28, 1.46, 1.64, 1.82, 2. ]),
...
array([ 1.5, 1.6, 1.7, 1.8, 1.9, 2. ])]
要连接它们,您必须删除每个数组的第一个元素或删除每个数组c
的最后一个元素b
。如果您只需要连接,我建议保持c
如上,并做:
>>> b = [np.linspace(0, j, 10, endpoint=False) for j in a]
>>> d = map(np.concatenate, zip(b, c))
>>> d
[array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.2, 1.4, 1.6, 1.8, 2. ]),
array([ 0. , 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88,
0.99, 1.1 , 1.28, 1.46, 1.64, 1.82, 2. ]),
...
array([ 0. , 0.15, 0.3 , 0.45, 0.6 , 0.75, 0.9 , 1.05, 1.2 ,
1.35, 1.5 , 1.6 , 1.7 , 1.8 , 1.9 , 2. ])]
如果你想要列表而不是 numpy 数组,你总是可以做一个 final
>>> d = map(list, d)