如何使用tqdm第三方库中的进度条模块在Python中实现进度跟踪?

2026-06-09 12:325阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计408个文字,预计阅读时间需要2分钟。

如何使用tqdm第三方库中的进度条模块在Python中实现进度跟踪?

使用方法一:`+tqdm(tqdm(list))+方法:`from tqdm import tqdmfrom ti`

使用方法一: tqdm

tqdm(list)方法可以传入任意一种list,比如数组,同时tqdm中不仅仅可以传入list, 同时可以传入所有带len方法的可迭代对象,这里只以list对象为例:


from tqdm import tqdm
from time import sleep

for i in tqdm(range(1000)):
sleep(0.1)


或是:

from tqdm import tqdm
from time import sleep

for i in tqdm(['a', 'b', 'c', 'd', 'e']):
sleep(0.1)


使用方法二: trange

trange(i) 是 tqdm(range(i)) 的等价写法

from tqdm import trange
from time import sleep

for i in trange(1000):
sleep(1)


如何使用tqdm第三方库中的进度条模块在Python中实现进度跟踪?

使用方法三: 改变循环信息

from tqdm import trange, tqdm
from time import sleep


pbar = tqdm(range(1000))
for char in pbar:
pbar.set_description("Processing %s" % char)
sleep(1)

或是:

from tqdm import trange, tqdm
from time import sleep


pbar = trange(1000)
for char in pbar:
pbar.set_description("Processing %s" % char)
sleep(1)

或是:

from tqdm import trange, tqdm
from time import sleep


for i in tqdm(range(100), desc='1st loop'):
sleep(1)




实际操作中发现 desc(str) 比 ​​set_description 好用。​​

使用方法四 手动控制进度:

import time
from tqdm import tqdm

# 一共200个,每次更新10,一共更新20次
with tqdm(total=200) as pbar:
for i in range(20):
pbar.update(10)
time.sleep(0.1)

或是:

pbar = tqdm(total=200)
for i in range(20):
pbar.update(10)
time.sleep(0.1)
# close() 不要也没出问题
pbar.close()


本文共计408个文字,预计阅读时间需要2分钟。

如何使用tqdm第三方库中的进度条模块在Python中实现进度跟踪?

使用方法一:`+tqdm(tqdm(list))+方法:`from tqdm import tqdmfrom ti`

使用方法一: tqdm

tqdm(list)方法可以传入任意一种list,比如数组,同时tqdm中不仅仅可以传入list, 同时可以传入所有带len方法的可迭代对象,这里只以list对象为例:


from tqdm import tqdm
from time import sleep

for i in tqdm(range(1000)):
sleep(0.1)


或是:

from tqdm import tqdm
from time import sleep

for i in tqdm(['a', 'b', 'c', 'd', 'e']):
sleep(0.1)


使用方法二: trange

trange(i) 是 tqdm(range(i)) 的等价写法

from tqdm import trange
from time import sleep

for i in trange(1000):
sleep(1)


如何使用tqdm第三方库中的进度条模块在Python中实现进度跟踪?

使用方法三: 改变循环信息

from tqdm import trange, tqdm
from time import sleep


pbar = tqdm(range(1000))
for char in pbar:
pbar.set_description("Processing %s" % char)
sleep(1)

或是:

from tqdm import trange, tqdm
from time import sleep


pbar = trange(1000)
for char in pbar:
pbar.set_description("Processing %s" % char)
sleep(1)

或是:

from tqdm import trange, tqdm
from time import sleep


for i in tqdm(range(100), desc='1st loop'):
sleep(1)




实际操作中发现 desc(str) 比 ​​set_description 好用。​​

使用方法四 手动控制进度:

import time
from tqdm import tqdm

# 一共200个,每次更新10,一共更新20次
with tqdm(total=200) as pbar:
for i in range(20):
pbar.update(10)
time.sleep(0.1)

或是:

pbar = tqdm(total=200)
for i in range(20):
pbar.update(10)
time.sleep(0.1)
# close() 不要也没出问题
pbar.close()