简体   繁体   中英

Progress bar with tqdm while iterating over the items in a python dictionary

If I'm trying to get a progress bar while iterating over a dict, how can I do this with tqdm? I'm using Python 2.7.

This works great with lists:

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

But fails over dicts:

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

What's the proper way to do this with dicts?

Here's a real example:

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

I get:

-------------------------------------------------------------------
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__'

Your usage of tqdm is wrong. The second argument you're specifying is desc according to the documentation . You need to use a string as a second positional argument. If you want to use the total argument as second, you should pass it with its key like that: for k, v in tqdm.tqdm(d.items(),total=len(d)): So basically, it works as usual for dictionaries as for lists:

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

You can then use optional arguments, refer to the link

tqdm is the most popular option, there are other options one of which is progress_bar progress_bar is really simple and adapts well to different situations. You only need to wrap your iterator inside and that's it.

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

Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM