通过预定义的步骤遍历列表

时间:2019-04-03 12:48:48

标签: python list integral

如何用定义的步骤遍历列表,而不是在列表的每个项目中,我希望能够跳过一些项目。

t = np.linspace(0,100,1000)
Fs = 6000
f = 200

func = 50*np.sin(2 * np.pi * f * t / Fs)+50


idx = [9 140 309 439 609 739 908]
for i in (idx):
        it = integrate.simps(func[i], t[i])
        print(it)

>> gives me this :
5428.010261680331
5428.010261680331
5428.010261680331
5428.010261680331
5428.010261680331
5428.010261680331
5428.010261680331

''' wich is logical, because it itterates over every element,
but how to itterate like the following : 
i want to do integral of func from:
idx 1 to 2
idx 3 to 4 
idx 5 to 6
idx 7 to end wich dosent exist because the list is just 7 items
  so that it gives me only foor values'''

1 个答案:

答案 0 :(得分:0)

您要成对遍历idx中的值吗?

这篇帖子Iterating over every two elements in a list具有一些实现方法。不确定如何处理最后一个没有下一个元素的边缘情况。