国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python的Tqdm模塊實現進度條配置

瀏覽:99日期:2022-06-27 10:55:26

tqdm官網地址:https://pypi.org/project/tqdm/

Github地址:https://github.com/tqdm/tqdm

簡介

Tqdm 是一個快速,可擴展的Python進度條,可以在 Python 長循環中添加一個進度提示信息,用戶只需要封裝任意的迭代器 tqdm(iterator)。

總之,它是用來顯示進度條的,很漂亮,使用很直觀(在循環體里邊加個tqdm),而且基本不影響原程序效率。名副其實的“太強太美”了!這樣在寫運行時間很長的程序時,是該多么舒服啊!

給一張GIF圖看一下實際效果

Python的Tqdm模塊實現進度條配置

安裝

pip install tqdm 使用

示例一

簡單的demo:

# !/user/bin/env python# -*- coding:utf-8 -*- import timefrom tqdm import tqdmfrom tqdm._tqdm import trange for i in tqdm(range(100)): time.sleep(0.01)

輸出結果如下:

Python的Tqdm模塊實現進度條配置

關于tqdm對于range的封裝

import timefrom tqdm import tqdmfrom tqdm._tqdm import trange for j in trange(100): time.sleep(0.1)

輸出結果如下(同上)

Python的Tqdm模塊實現進度條配置

示例二:

對于任意list的使用

alist = list(’letters’)bar = tqdm(alist)for letter in bar: bar.set_description(f'Now get {letter}')

輸出結果如下:

Python的Tqdm模塊實現進度條配置

傳入任意list

pbar = tqdm(['a', 'b', 'c', 'd']) for char in pbar: pbar.set_description('Processing %s' % char)

手動控制更新

with tqdm(total=100) as pbar: for i in range(10): pbar.update(10) # 也可以這樣pbar = tqdm(total=100)for i in range(10): pbar.update(10)pbar.close()

示例三:

結合pandas的使用

import pandas as pdimport 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)

輸出結果如下:

Python的Tqdm模塊實現進度條配置

示例四

在Shell的tqdm用法

$ time find . -name ’*.py’ -exec cat {} ; | wc -l857365 real 0m3.458suser 0m0.274ssys 0m3.325s $ time find . -name ’*.py’ -exec cat {} ; | tqdm | wc -l857366it [00:03, 246471.31it/s]857365 real 0m3.585suser 0m0.862ssys 0m3.358s

使用的參數:

$ find . -name ’*.py’ -exec cat {} ; | tqdm --unit loc --unit_scale --total 857366 >> /dev/null100%|???????????????????????????????????| 857K/857K [00:04<00:00, 246Kloc/s]

備份一個目錄:

$ 7z a -bd -r backup.7z docs/ | grep Compressing | tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log100%|????????????????????????????????| 8014/8014 [01:37<00:00, 82.29files/s]

本文參考:https://blog.csdn.net/langb2014/article/details/54798823?locationnum=8&fps=1

到此這篇關于Python的Tqdm模塊實現進度條配置的文章就介紹到這了,更多相關Python Tqdm進度條內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 郓城县| 翼城县| 寻乌县| 咸丰县| 牡丹江市| 富蕴县| 伊春市| 吉林省| 图们市| 那坡县| 聂拉木县| 黄浦区| 达孜县| 湖北省| 五峰| 鹰潭市| 田阳县| 弥渡县| 白城市| 岳阳县| 莱阳市| 界首市| 鄱阳县| 黄冈市| 儋州市| 商城县| 无棣县| 滦南县| 青神县| 醴陵市| 南靖县| 中西区| 江华| 齐齐哈尔市| 蚌埠市| 靖边县| 城市| 微博| 靖江市| 庆城县| 正蓝旗|