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 @   VipSoft  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2023-01-16 Axure 多人协作
2023-01-16 Axure 母版红色怎么去除?
2014-01-16 C# 指定物理目录下载文件,Response.End导致“正在中止线程”异常的问题
点击右上角即可分享
微信分享提示