链式可迭代对象的tqdm进度栏

如何解决链式可迭代对象的tqdm进度栏

如果我想在Python中组合两个迭代器,一种方法是使用itertools.chain

例如,如果我有两个范围df['new_column'] = df.column1.apply(lambda x: len(str(x).split(' '))) column 1 Camiseta Tecnica hombre barata deportivas calcetin hombres running Camiseta Tecnica mejores deportivas running Camiseta ,则可以用range(50,100,10)得到一个范围range(95,101)

[50,60,70,80,90,95,96,97,98,99,100]是Python中的可扩展进度条。但是,认情况下,即使固定了itertools.chain(range(50,10),range(95,101))表达式,它似乎也无法计数。

一种解决方案是将范围转换为列表。但是,这种方法无法扩展。

是否可以确保tqdm理解链式迭代器?

itertools.chain

解决方法

这与其说是一种解决方法,不如说是一种解决方法,因为看起来 tqdm 现在无法处理它。但是,您只需找到链接在一起的两个事物的长度,然后在调用 total= 时包含参数 tqdm

from tqdm import tqdm
from itertools import chain

# I started with a list of iterables
iterables = [iterable1,iterable2,...]

# Get the length of each iterable and then sum them all
total_len = sum([len(i) for i in iterables])

# Then chain them together
main_iterable = chain(*iterables)

# And finally,run it through tqdm
# Make sure to use the `total=` argument
for thing in tqdm(main_iterable,total=total_len):
    # Do stuff

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?