【问题标题】:Progress bar with tqdm while iterating over the items in a python dictionary迭代python字典中的项目时带有tqdm的进度条
【发布时间】:2018-09-21 18:47:38
【问题描述】:

如果我试图在迭代字典时获得进度条,我该如何使用 tqdm 来做到这一点?我正在使用 Python 2.7。

这对列表很有效:

for i in tdqm(l, len(l):
    <do stuff>   

但故障转移 dicts:

for k, v in tqdm(d.items(), len(d)):
   <do stuff>

用字典来做这件事的正确方法是什么?

这是一个真实的例子:

d = {'k1':1, 'k2':2}
for k, v in tqdm(d.items(), len(d)):
    print 'foo'
    a = 1 + 100
    print 'bar'

我明白了:

-------------------------------------------------------------------
TypeError                         Traceback (most recent call last)
<ipython-input-30-7e4ce2b85414> in <module>()
      1 d = {'k1':1, 'k2':2}
----> 2 for k, v in tqdm(d.items(), len(d)):
      3     print 'oasdlkfj'
      4     a = 1 + 100
      5     print 'y'

/home/monica/anaconda2/envs/pytorch_p27/lib/python2.7/site-packages/tqdm/_tqdm.pyc in __init__(self, iterable, desc, total, leave, file, ncols, mininterval, maxinterval, miniters, ascii, disable, unit, unit_scale, dynamic_ncols, smoothing, bar_format, initial, position, postfix, unit_divisor, gui, **kwargs)
    810                 if self.pos:
    811                     self.moveto(self.pos)
--> 812                 self.sp(self.__repr__(elapsed=0))
    813                 if self.pos:
    814                     self.moveto(-self.pos)

/home/monica/anaconda2/envs/pytorch_p27/lib/python2.7/site-packages/tqdm/_tqdm.pyc in __repr__(self, elapsed)
    842             self.desc, self.ascii, self.unit,
    843             self.unit_scale, 1 / self.avg_time if self.avg_time else None,
--> 844             self.bar_format, self.postfix, self.unit_divisor)
    845 
    846     def __lt__(self, other):

/home/monica/anaconda2/envs/pytorch_p27/lib/python2.7/site-packages/tqdm/_tqdm.pyc in format_meter(n, total, elapsed, ncols, prefix, ascii, unit, unit_scale, rate, bar_format, postfix, unit_divisor)
    288             if prefix:
    289                 # old prefix setup work around
--> 290                 bool_prefix_colon_already = (prefix[-2:] == ": ")
    291                 l_bar = prefix if bool_prefix_colon_already else prefix + ": "
    292             else:

TypeError: 'int' object has no attribute '__getitem__'

【问题讨论】:

    标签: python dictionary iteration items tqdm


    【解决方案1】:

    您对tqdm 的使用是错误的。根据documentation,您指定的第二个参数是desc。您需要使用字符串作为第二个位置参数。如果您想使用总参数作为第二个参数,您应该使用它的键传递它,如下所示: for k, v in tqdm.tqdm(d.items(),total=len(d)): 所以基本上,它对字典和列表一样适用:

    for k, v in tqdm.tqdm(d.items()): do stuff

    然后你可以使用可选参数,参考链接

    【讨论】:

      【解决方案2】:

      tqdm 是最受欢迎的选项,还有其他选项,其中之一是 progress_bar progress_bar 非常简单,可以很好地适应不同的情况。你只需要把你的迭代器包装在里面就可以了。

      from fastprogress.fastprogress import progress_bar
      {key:value for key, value in progress_bar(dict.items())}
      

      Documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-14
        • 1970-01-01
        • 1970-01-01
        • 2021-03-08
        • 2018-07-04
        • 1970-01-01
        相关资源
        最近更新 更多