In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use the progress bar in Python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the progress bar in Python" can help you solve the problem.
Tqdm can perfectly support and solve this problem. It is a fast and scalable progress bar library. Users only need to encapsulate any iterator tqdm (iterator) to add a progress message to the Python long loop.
Installation:
Pip install tqdm based on iterator usage
[example] use tqdm (iterator)
Import timefrom tqdm import tqdmfor i in tqdm (range): time.sleep (0.05for i in tqdm) (list ('abcdefgh')): time.sleep (0.05for i in tqdm) for i in tqdm (range, desc='Processing'): time.sleep
[example] trange (N) is a simple way to write tqdm (range (N))
Import timefrom tqdm import tqdm, trangefor i in trange: time.sleep
Instantiation outside the loop allows manual control of tqdm ()
Import timefrom tqdm import tqdmpbar = tqdm (['Processing', 'baked,' cased, 'dashed,' eyed, 'faded,' gelled,'h']) for i in pbar: pbar.set_description ('Processing' + I) time.sleep (0.2)
[example]
Import timefrom tqdm import tqdmfrom random import random, randintwith tqdm (range) as pbar: for i in pbar: pbar.set_description ("GEN% d"% I) pbar.set_postfix ({'loss': random (),' gen': randint (1,999)}) time.sleep (0.1) updates based on manual
[example] use with statement to manually control tqdm () update
Import timefrom tqdm import tqdmwith tqdm (total=200) as pbar: pbar.set_description ("Processing") for i in range (20): time.sleep (0.1) pbar.update (10)
If the optional variable total (or iterable with len ()) is provided, forecast statistics are displayed.
With is also optional (you can assign tqdm () to a variable, but in this case, don't forget to del or close () at the end.
Import timefrom tqdm import tqdmpbar = tqdm (total=200) pbar.set_description ("Processing") for i in range (20): time.sleep (0.1) pbar.update (10) pbar.close () tqdm module parameter description class tqdm (Comparable): "" Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested. "" Def set_description (self, desc=None, refresh=True): def set_postfix (self, ordered_dict=None, refresh=True, * * kwargs): def update (self, self): def close (self):
The set_description () function: a description for setting / modifying the progress bar.
Set_postfix () function: used to set / modify suffixes (additional statistics).
Update () function: update the progress bar manually.
The close () function clears and closes progressbar.
Class tqdm (Comparable): "Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested." Def _ init__ (self, iterable=None, desc=None, total=None, leave=False, file=sys.stderr, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, nested=False, bar_format=None, initial=0, gui=False):
Iterable: iterable objects that do not need to be set when manually updated.
Desc: string, progress bar description text on the left.
Total: total number of projects.
Leave: Bool value, whether to keep the progress bar after the iteration is completed.
File: the output points to the location. The default is the terminal, which generally does not need to be set.
Ncols: adjust the width of the progress bar. The default is to automatically adjust the length according to the environment. If set to 0, there is no progress bar, only the output information.
Unit: describes the text used to process the project. The default is' it', for example: 100 it/s, and if you are dealing with photos, it is set to 'img', then it is 100 img/s.
Unit_scale: automatically converts project processing speed units according to international standards, such as 100000 it/s > > 100k it/s.
[example]
Import timefrom tqdm import tqdmwith tqdm (total=100000, desc='Example', leave=True, ncols=100, unit='B', unit_scale=True) as pbar: for i in range (10): time.sleep (0.5) pbar.update (10000) that's all for "how Python uses the progress bar". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.