Python進(jìn)度條的使用
在使用Python處理比較耗時操作的時候,為了便于觀察處理進(jìn)度,就需要通過進(jìn)度條將處理情況進(jìn)行可視化展示,以便我們能夠及時了解情況。這對于第三方庫非常豐富的Python來說,并不是什么難事。
tqdm就能非常完美的支持和解決這個問題,它是一個快速、擴展性強的進(jìn)度條工具庫。用戶只需要封裝任意的迭代器 tqdm(iterator),就能在 Python 長循環(huán)中添加一個進(jìn)度提示信息。
官網(wǎng):
安裝:
pip install tqdm
基于迭代器的使用方式
【例子】使用tqdm(iterator)
import time
from tqdm import tqdm
for i in tqdm(range(100)):
time.sleep(0.05)
for i in tqdm(list('abcdefgh')):
time.sleep(0.05)
for i in tqdm(range(100), desc='Processing'):
time.sleep(0.05)

【例子】trange(N)是tqdm(range(N))的一種簡單寫法
import time
from tqdm import tqdm, trange
for i in trange(100):
time.sleep(0.05)

【例子】循環(huán)外的實例化允許手動控制tqdm()
import time
from tqdm import tqdm
pbar = tqdm(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
for i in pbar:
pbar.set_description('Processing ' + i)
time.sleep(0.2)

【例子】
import time
from tqdm import tqdm
from random import random, randint
with tqdm(range(100)) 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)

基于手動進(jìn)行更新
【例子】使用with語句手動控制tqdm()更新
import time
from tqdm import tqdm
with tqdm(total=200) as pbar:
pbar.set_description("Processing")
for i in range(20):
time.sleep(0.1)
pbar.update(10)

如果提供了可選變量total(或帶有len()的iterable),則會顯示預(yù)測統(tǒng)計信息。
with也是可選的(可以將tqdm()賦值給變量,但在這種情況下,不要忘記在結(jié)尾處del或close()。
import time
from tqdm import tqdm
pbar = tqdm(total=200)
pbar.set_description("Processing")
for i in range(20):
time.sleep(0.1)
pbar.update(10)
pbar.close()

tqdm模塊參數(shù)說明
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, n=1):
def close(self):
set_description()函數(shù):用于設(shè)置/修改進(jìn)度條的說明。set_postfix()函數(shù):用于設(shè)置/修改后綴(附加統(tǒng)計信息)。update()函數(shù):手動更新進(jìn)度條。close()函數(shù):清除并關(guān)閉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:可迭代的對象,在手動更新時不需要進(jìn)行設(shè)置。
- desc:字符串,左邊進(jìn)度條描述文字。
- total:總的項目數(shù)。
- leave:bool值,迭代完成后是否保留進(jìn)度條。
- file:輸出指向位置,默認(rèn)是終端, 一般不需要設(shè)置。
- ncols:調(diào)整進(jìn)度條寬度,默認(rèn)是根據(jù)環(huán)境自動調(diào)節(jié)長度,如果設(shè)置為0,就沒有進(jìn)度條,只有輸出的信息。
- unit:描述處理項目的文字,默認(rèn)是'it',例如: 100 it/s,處理照片的話設(shè)置為'img' ,則為 100 img/s。
- unit_scale:自動根據(jù)國際標(biāo)準(zhǔn)進(jìn)行項目處理速度單位的換算,例如 100000 it/s >> 100k it/s。
【例子】
import time
from tqdm import tqdm
with 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)

tqdm源自阿拉伯語單詞taqaddum,意思是“progress(進(jìn)展)”,是python中一個快速、擴展性強的進(jìn)度條工具庫,能讓我們了解代碼的運行進(jìn)度,也能讓我們的運行結(jié)果看起來顯得更加美觀而又高大上?。?喜歡的小伙伴趕緊用起來吧??!
到此這篇關(guān)于Python進(jìn)度條的使用的文章就介紹到這了,更多相關(guān)Python進(jìn)度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)分析處理(三)--運動員信息的分組與聚合
這篇文章主要介紹了Python數(shù)據(jù)清洗與處理?運動員信息的分組與聚合,根據(jù)Python數(shù)據(jù)清洗與處理?的相關(guān)資料展開運動員信息的分組與聚合的文章內(nèi)容,需要的朋友可以參考一下2021-12-12
Python在信息學(xué)競賽中的運用及Python的基本用法(詳解)
下面小編就為大家?guī)硪黄狿ython在信息學(xué)競賽中的運用及Python的基本用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
pytorch和numpy默認(rèn)浮點類型位數(shù)詳解
這篇文章主要介紹了pytorch和numpy默認(rèn)浮點類型位數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python實現(xiàn)鼠標(biāo)自動在屏幕上隨機移動功能
這篇文章主要介紹了Python實現(xiàn)鼠標(biāo)自動在屏幕上隨機移動功能,具有很好的參考價值,希望對大家有所幫助。還等什么?一起跟隨小編過來看看吧2020-03-03
opencv-python+yolov3實現(xiàn)目標(biāo)檢測
因為最近的任務(wù)有用到目標(biāo)檢測,快速地了解了目標(biāo)檢測這一任務(wù),并且實現(xiàn)了使用opencv進(jìn)行目標(biāo)檢測。感興趣的可以了解一下2021-06-06

