tqdm(list)方法可以传入任意一种list,比如数组,同时tqdm中不仅仅可以传入list, 同时可以传入所有带len方法的可迭代对象,这里只以list对象为例:
from tqdm import tqdm
from time import sleep
for i in tqdm(range(1000)):
sleep(0.1)
61%|██████▏ | 614/1000 [01:01<00:38, 9.93it/s]
或是:
from tqdm import tqdm
from time import sleep
for i in tqdm(['a', 'b', 'c', 'd', 'e']):
sleep(0.1)
0%| | 0/5 [00:00, ?it/s] 20%|██ | 1/5 [00:00<00:00, 9.99it/s] 40%|████ | 2/5 [00:00<00:00, 9.94it/s] 60%|██████ | 3/5 [00:00<00:00, 9.92it/s] 80%|████████ | 4/5 [00:00<00:00, 9.91it/s] 100%|██████████| 5/5 [00:00<00:00, 9.90it/s]
trange(i) 是 tqdm(range(i)) 的等价写法
from tqdm import trange
from time import sleep
for i in trange(10):
sleep(1)
0%| | 0/10 [00:00, ?it/s] 10%|█ | 1/10 [00:01<00:09, 1.00s/it] 20%|██ | 2/10 [00:02<00:08, 1.00s/it] 30%|███ | 3/10 [00:03<00:07, 1.00s/it] 40%|████ | 4/10 [00:04<00:06, 1.00s/it] 50%|█████ | 5/10 [00:05<00:05, 1.00s/it] 60%|██████ | 6/10 [00:06<00:04, 1.00s/it] 70%|███████ | 7/10 [00:07<00:03, 1.00s/it] 80%|████████ | 8/10 [00:08<00:02, 1.00s/it] 90%|█████████ | 9/10 [00:09<00:01, 1.00s/it] 100%|██████████| 10/10 [00:10<00:00, 1.00s/it]
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(10)
for char in pbar:
pbar.set_description("Processing %s" % char)
sleep(1)
0%| | 0/10 [00:00, ?it/s] Processing 0: 0%| | 0/10 [00:00, ?it/s] Processing 0: 10%|█ | 1/10 [00:01<00:09, 1.00s/it] Processing 1: 10%|█ | 1/10 [00:01<00:09, 1.00s/it] Processing 1: 20%|██ | 2/10 [00:02<00:08, 1.00s/it] Processing 2: 20%|██ | 2/10 [00:02<00:08, 1.00s/it] Processing 2: 30%|███ | 3/10 [00:03<00:07, 1.00s/it] Processing 3: 30%|███ | 3/10 [00:03<00:07, 1.00s/it] Processing 3: 40%|████ | 4/10 [00:04<00:06, 1.00s/it] Processing 4: 40%|████ | 4/10 [00:04<00:06, 1.00s/it] Processing 4: 50%|█████ | 5/10 [00:05<00:05, 1.00s/it] Processing 5: 50%|█████ | 5/10 [00:05<00:05, 1.00s/it] Processing 5: 60%|██████ | 6/10 [00:06<00:04, 1.00s/it] Processing 6: 60%|██████ | 6/10 [00:06<00:04, 1.00s/it] Processing 6: 70%|███████ | 7/10 [00:07<00:03, 1.00s/it] Processing 7: 70%|███████ | 7/10 [00:07<00:03, 1.00s/it] Processing 7: 80%|████████ | 8/10 [00:08<00:02, 1.00s/it] Processing 8: 80%|████████ | 8/10 [00:08<00:02, 1.00s/it] Processing 8: 90%|█████████ | 9/10 [00:09<00:01, 1.00s/it] Processing 9: 90%|█████████ | 9/10 [00:09<00:01, 1.00s/it] Processing 9: 100%|██████████| 10/10 [00:10<00:00, 1.00s/it]
或是
from tqdm import trange, tqdm
from time import sleep
for i in tqdm(range(100), desc='1st loop'):
sleep(1)
1st loop: 0%| | 0/10 [00:00, ?it/s] 1st loop: 10%|█ | 1/10 [00:01<00:09, 1.00s/it] 1st loop: 20%|██ | 2/10 [00:02<00:08, 1.00s/it] 1st loop: 30%|███ | 3/10 [00:03<00:07, 1.00s/it] 1st loop: 40%|████ | 4/10 [00:04<00:06, 1.00s/it] 1st loop: 50%|█████ | 5/10 [00:05<00:05, 1.00s/it] 1st loop: 60%|██████ | 6/10 [00:06<00:04, 1.00s/it] 1st loop: 70%|███████ | 7/10 [00:07<00:03, 1.00s/it] 1st loop: 80%|████████ | 8/10 [00:08<00:02, 1.00s/it] 1st loop: 90%|█████████ | 9/10 [00:09<00:01, 1.00s/it] 1st loop: 100%|██████████| 10/10 [00:10<00:00, 1.00s/it]
实际操作中发现 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)
0%| | 0/200 [00:00, ?it/s] 10%|█ | 20/200 [00:00<00:00, 199.67it/s] 15%|█▌ | 30/200 [00:00<00:01, 152.75it/s] 20%|██ | 40/200 [00:00<00:01, 131.22it/s] 25%|██▌ | 50/200 [00:00<00:01, 119.39it/s] 30%|███ | 60/200 [00:00<00:01, 112.31it/s] 35%|███▌ | 70/200 [00:00<00:01, 107.73it/s] 40%|████ | 80/200 [00:00<00:01, 104.89it/s] 45%|████▌ | 90/200 [00:00<00:01, 102.83it/s] 50%|█████ | 100/200 [00:00<00:00, 101.52it/s] 55%|█████▌ | 110/200 [00:01<00:00, 100.63it/s] 60%|██████ | 120/200 [00:01<00:00, 100.04it/s] 65%|██████▌ | 130/200 [00:01<00:00, 99.54it/s] 70%|███████ | 140/200 [00:01<00:00, 99.23it/s] 75%|███████▌ | 150/200 [00:01<00:00, 99.02it/s] 80%|████████ | 160/200 [00:01<00:00, 98.85it/s] 85%|████████▌ | 170/200 [00:01<00:00, 98.83it/s] 90%|█████████ | 180/200 [00:01<00:00, 98.75it/s] 95%|█████████▌| 190/200 [00:01<00:00, 98.71it/s] 100%|██████████| 200/200 [00:01<00:00, 98.56it/s]
或是:
pbar = tqdm(total=200)
for i in range(20):
pbar.update(10)
time.sleep(0.1)
# close() 不要也没出问题
pbar.close()
0%| | 0/200 [00:00, ?it/s] 10%|█ | 20/200 [00:00<00:00, 199.65it/s] 15%|█▌ | 30/200 [00:00<00:01, 152.73it/s] 20%|██ | 40/200 [00:00<00:01, 130.98it/s] 25%|██▌ | 50/200 [00:00<00:01, 119.20it/s] 30%|███ | 60/200 [00:00<00:01, 112.22it/s] 35%|███▌ | 70/200 [00:00<00:01, 107.76it/s] 40%|████ | 80/200 [00:00<00:01, 104.90it/s] 45%|████▌ | 90/200 [00:00<00:01, 103.01it/s] 50%|█████ | 100/200 [00:00<00:00, 101.63it/s] 55%|█████▌ | 110/200 [00:01<00:00, 100.74it/s] 60%|██████ | 120/200 [00:01<00:00, 100.14it/s] 65%|██████▌ | 130/200 [00:01<00:00, 99.60it/s] 70%|███████ | 140/200 [00:01<00:00, 99.27it/s] 75%|███████▌ | 150/200 [00:01<00:00, 99.07it/s] 80%|████████ | 160/200 [00:01<00:00, 98.57it/s] 85%|████████▌ | 170/200 [00:01<00:00, 98.61it/s] 90%|█████████ | 180/200 [00:01<00:00, 98.62it/s] 95%|█████████▌| 190/200 [00:01<00:00, 98.63it/s] 100%|██████████| 200/200 [00:01<00:00, 98.63it/s]
可以显示循环的进度条的库,再也不用担心不知道程序跑到哪里还要跑多久了
tqdm()可以直接包裹iterable的对象
from tqdm import tqdm,trange
from time import sleep
text = ""
for char in tqdm(["a", "b", "c", "d"]):
text = text + char
sleep(0.1)
0%| | 0/4 [00:00, ?it/s] 25%|██▌ | 1/4 [00:00<00:00, 9.99it/s] 50%|█████ | 2/4 [00:00<00:00, 9.95it/s] 75%|███████▌ | 3/4 [00:00<00:00, 9.93it/s] 100%|██████████| 4/4 [00:00<00:00, 9.91it/s]
trange(i)相当于tqdm(range(i))
for i in trange(100):
sleep(0.01)
可以在循环外面预先定义tqdm的对象
pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
pbar.set_description("Processing %s" % char)
有两个参数比较有用,desc(str)和leave(bool)
desc可以指定这个循环的的信息,以便区分。上面的set_description(str)和这个应该是一样的。
leave则表示进度条跑完了之后是否继续保留
for i in tqdm(range(10), desc='1st loop'):
for j in trange(100, desc='2nd loop', leave=False):
sleep(0.01)
如果要在Jupyter Notebook上面使用,那么要把tqdm换成tqdm_notebook,trange换成tnrange
from tqdm import tnrange, tqdm_notebook
from time import sleep
for i in tqdm_notebook(range(10), desc='1st loop'):
for j in tnrange(100, desc='2nd loop', leave=False):
sleep(0.01)
参考网址
https://blog.csdn.net/zejianli/article/details/77915751
https://www.cnblogs.com/devilmaycry812839668/p/10203895.html