python查看迭代器可迭代次数

使用more_itertools

这里要使用到more_itertools这个包,直接用pip安装就可以了

pip install more_itertools

查看迭代器可迭代的次数

import more_itertools
# data是你的迭代器
print(more_itertools.ilen(data))

转换可看长度对象

你也可以将你的迭代器变成一个列表,再查看列表长度。但是这样你的迭代器就变成空的了,建议使用第一种方法。

# data是你的迭代器

print(len(list(data)))
原文地址:https://www.cnblogs.com/Jaryer/p/13644358.html