请教 Python 怎么进行列表的插值?

2019-08-08 16:04:26 +08:00
 oldbird

比如[a,b,c,d,a],希望得到 [a,(a+b)/2,b,(b+c)/2,c,(c+d)/2,d,(d+a)/2,a] 不知道有没有现成的库?

3286 次点击
所在节点    Python
12 条回复
Ziya
2019-08-08 16:10:16 +08:00
oldbird
2019-08-08 16:18:40 +08:00
@Ziya 不是想知道 insert 方法
junkun
2019-08-08 16:20:31 +08:00
numpy.interp?
hjq98765
2019-08-08 16:30:05 +08:00
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:])]
smr1113
2019-08-08 17:23:44 +08:00
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})
smr1113
2019-08-08 17:34:25 +08:00
看错了~~
python:
from itertools import chain
list(chain(*zip(x, [(i+j)/2.0 for i,j in zip(x,x[1:])]))) + [x[-1]]
zkqiang
2019-08-08 18:17:36 +08:00
这种规则的不应该插值,建议像楼上那样生成新的 list
necomancer
2019-08-09 04:20:36 +08:00
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)
necomancer
2019-08-09 04:54:18 +08:00
或者算符版的
b = np.vstack([a, np.convolve([.5,.5,0],a,'same')]).ravel('F')[:-1]
卷积里一个数组长度为常数所以应该还是 O(n) 的复杂度,不过 a 的长度必须大于等于 3,这样少生成两次用来插值的数组
altboy
2019-08-09 11:18:52 +08:00
这浏览器提醒的多明显
![不理智啊]( )
source
2019-08-09 11:56:54 +08:00
@altboy #10 老哥你回错帖了,这说的是闲鱼被骗那个贴吧
altboy
2019-08-09 13:12:50 +08:00
@source 我擦,回完就溜了,尴尬。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/590199

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX