tqdm(list)方法能够传入任意一种list,好比数组,同时tqdm中不单单能够传入list, 同时能够传入全部带len方法的可迭代对象,这里只以list对象为例:python
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(i) 是 tqdm(range(i)) 的等价写法oop
from tqdm import trange from time import sleep for i in trange(1000): sleep(1)
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)
或是:spa
from tqdm import trange, tqdm from time import sleep pbar = trange(1000) for char in pbar: pbar.set_description("Processing %s" % char) sleep(1)
或是:code
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)
或是:blog
pbar = tqdm(total=200) for i in range(20): pbar.update(10) time.sleep(0.1) # close() 不要也没出问题 pbar.close()