比如[a,b,c,d,a],希望得到 [a,(a+b)/2,b,(b+c)/2,c,(c+d)/2,d,(d+a)/2,a] 不知道有没有现成的库?
比如[a,b,c,d,a],希望得到 [a,(a+b)/2,b,(b+c)/2,c,(c+d)/2,d,(d+a)/2,a] 不知道有没有现成的库?
|
ZiyaAug 8, 2019
1
|
|
junkunAug 8, 2019
3
numpy.interp?
|
|
hjq98765Aug 8, 2019
4
3 楼正解?
我给个笨办法: import itertools as it a = range(9) print a b = list(it.chain.from_iterable([[x,x] for x in a])) print [(x+y)/2.0 for x,y in zip(b[:-1],b[1:])] |
|
smr1113Aug 8, 2019
5
python:[x[0]] + [(i+j)/2.0 for i,j in zip(x,x[1:])]
函数式: x.head :: (x zip x.tail map {case (x,y) => (x+y)/2.0}) |
|
smr1113Aug 8, 2019
6
看错了~~
python: from itertools import chain list(chain(*zip(x, [(i+j)/2.0 for i,j in zip(x,x[1:])]))) + [x[-1]] |
|
zkqiangAug 8, 2019
7
这种规则的不应该插值,建议像楼上那样生成新的 list
|
|
necomancerAug 9, 2019
8
numpy 版的:
b = np.interp(np.linspace(0,1,2*a.shape[0]-1,endpoint=1),np.linspace(0,1,a.shape[0],endpoint=1),a) |
|
necomancerAug 9, 2019
9
或者算符版的
b = np.vstack([a, np.convolve([.5,.5,0],a,'same')]).ravel('F')[:-1] 卷积里一个数组长度为常数所以应该还是 O(n) 的复杂度,不过 a 的长度必须大于等于 3,这样少生成两次用来插值的数组 |
|
altboyAug 9, 2019
10
这浏览器提醒的多明显
 |