擅长:python、mysql、java
<p>使用索引结果而不是命名变量,高度正则的数学表达式可能更清晰<br/>
Python非常喜欢使用迭代器</p>
<p>一旦您习惯了列出理解,<code>zip()</code>我认为生成一个diff列表可能会更清楚</p>
<pre><code>SetVel = [math.sin(math.pi*(n % 10 - 5)/5) for n in range(20)]
diffs = [[ e1 - e0, e2 - e1, e3 - e2, e4 - e3 ]
for e0, e1, e2, e3, e4
in zip(SetVel,
SetVel[1:],
SetVel[2:],
SetVel[3:],
SetVel[4:])]
OutSmooth = []
IndSmooth = []
delta = 1.5 # may want to change the constant, do it one place
for i, d in enumerate(diffs):
if d[1] < delta*d[0] or d[2] < delta*d[3]:
IndSmooth.append(i+2) # a guess at what you really wanted
else:
OutSmooth.append(SetVel[i+2])
IndSmooth
Out[64]: [2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17]
OutSmooth
Out[65]: [0.0, 0.0]
</code></pre>
<p>一个常见的编程目标是参数化、自动化,所以这里的输入是您想要的差异数,其余的是自动的</p>
^{pr2}$