我想通过一个迭代器反复(N 次)循环遍历一个列表,以免在内存中实际存储 N 个列表副本.有没有一种内置的或优雅的方法可以在不编写我自己的生成器的情况下做到这一点?
I'd like to cycle through a list repeatedly (N times) via an iterator, so as not to actually store N copies of the list in memory. Is there a built-in or elegant way to do this without writing my own generator?
理想情况下, itertools.cycle(my_list) 会有第二个参数来限制它循环的次数......唉,没有这样的运气.
Ideally, itertools.cycle(my_list) would have a second argument to limit how many times it cycles... alas, no such luck.
import itertools
itertools.chain.from_iterable(itertools.repeat([1, 2, 3], 5))
Itertools 是一个很棒的库.:)
Itertools is a wonderful library. :)
这篇关于有没有一种优雅的方法可以通过迭代循环遍历列表 N 次(如 itertools.cycle 但限制循环)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)