Python 進度條 tqdm模塊

tqdm官網地址:https://pypi.org/project/tqdm/
Github地址:https://github.com/tqdm/tqdm

tqdm 是一個快速,可擴展的Python進度條,可以在 Python 長循環中添加一個進度提示信息,用戶只需要封裝任意的迭代器 tqdm(iterator)。

安裝

pip install tqdm

簡單使用

import time
from tqdm import tqdm

for i in tqdm(range(100)):
    time.sleep(0.01)

image

tqdm對於range的封裝

import time 
from tqdm._tqdm import trange

for j in trange(100):
    time.sleep(0.1)

image

list的使用

import time
from tqdm import tqdm

alist = list('letters')
bar = tqdm(alist)
for letter in bar:
    time.sleep(0.5)
    bar.set_description(f"Now get {letter}")

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    time.sleep(0.5)
    pbar.set_description("Processing %s" % char)

image

import time
from tqdm import tqdm

with tqdm(total=100) as pbar:
    for i in range(10):
        time.sleep(0.5)
        pbar.update(10)

# 也可以這樣
pbar = tqdm(total=100)
for i in range(10):
    time.sleep(0.5)
    pbar.update(10)
pbar.close()

image

pandas 使用

import time
from tqdm import tqdm

import pandas as pd
import numpy as  np

df = pd.DataFrame(np.random.randint(0, 100, (10000000, 6)))
tqdm.pandas(desc="my bar!")
df.progress_apply(lambda x: x ** 2)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章