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)
posted @ 2024-01-16 16:43  VipSoft  阅读(5)  评论(0编辑  收藏  举报