Python 如何在for循环中从数组(列表)中获取下一个项目[重复]
2022/6/22 1:19:48
本文主要是介绍Python 如何在for循环中从数组(列表)中获取下一个项目[重复],对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
我有一个带有时间的数组(列表),现在我需要确定该Eclipse的时间是否在此列表的两个连续时间之间。
duurSeq=[11,16,22,29] nu = time.time() verstreken_tijd = nu - starttijd for t in duurSeq: if (verstreken_tijd > t ) and (verstreken_tijd < **t_next** ): doSomething():
我的问题是:如何获取t_next(for循环中数组中的下一项)
解决方案
duurSeq=[11,16,22,29] for c, n in zip(duurSeq, duurSeq[1:]): if (verstreken_tijd > c) and (verstreken_tijd < n): doSomething():
请参阅Python中的成对列表(当前,下一个)进行迭代,以了解一般方法。
from itertools import tee, izip def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return izip(a, b) # Demo l = [11, 16, 22, 29] for c, n in pairwise(l): print(c, n) # Output (11, 16) (16, 22) (22, 29)
这篇关于Python 如何在for循环中从数组(列表)中获取下一个项目[重复]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践