更优雅的字符串print——pprint库的使用
pprint是一个python下的输出格式化库,可以美化输出字符串的格式,主要是实现对字符串换行宽度、缩进、嵌套对象打印深度的控制,此外还可以实现对np数组实现更为紧凑的输出。下面对pprint库的使用做一个详细的介绍
1、安装pprint
pip install pprint
2、基本使用
mylist = ["Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.","It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.", "The proposed MLP decoder aggregates information from different layers, ", "and thus combining both local attention and global attention to render powerful representation"]
print(mylist)
pprint.pprint(mylist)
输出信息如下所示: ['Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.', 'It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.', 'The proposed MLP decoder aggregates information from different layers, ', 'and thus combining both local attention and global attention to render powerful representation'] ['Semantic segmentation is a fundamental task in computer vision and enables ''many downstream applications.','It is related to image classification since it produces per-pixel category ''prediction instead of image-level prediction.','The proposed MLP decoder aggregates information from different layers, ','and thus combining both local attention and global attention to render ''powerful representation']
3、设置字符缩进
#设置字符缩进,默认值0
pprint.pprint(mylist, indent=4)
输出信息如下所示: [ 'Semantic segmentation is a fundamental task in computer vision and ''enables many downstream applications.','It is related to image classification since it produces per-pixel ''category prediction instead of image-level prediction.','The proposed MLP decoder aggregates information from different layers, ','and thus combining both local attention and global attention to render ''powerful representation']
4、设置字符串换行宽度
#设置字符串换行宽度,默认值80个字符
pprint.pprint(mylist, width=150)
输出信息如下所示: ['Semantic segmentation is a fundamental task in computer vision and enables many downstream applications.','It is related to image classification since it produces per-pixel category prediction instead of image-level prediction.','The proposed MLP decoder aggregates information from different layers, ','and thus combining both local attention and global attention to render powerful representation']
5、设置输出数据的深度
#输出数据的深度控制,用于控制json、dict、list、set、tuple等包含嵌套的对象深度
newlist = [1, [2, [3, [4, [5, [6]]]]]]
print("print:\n",newlist)
print("pprint.pprint:")
pprint.pprint(newlist, depth=3)dic1={"a1":1,"b1":2,"dic":{"a2":1,"b2":2,"dic":{"a3":1,"b3":2,"dic":{"a4":1,"b4":2}}}}
print("print:\n",dic1)
print("pprint.pprint:")
pprint.pprint(dic1, depth=3,width=40)
输出信息如下所示: print:[1, [2, [3, [4, [5, [6]]]]]] pprint.pprint: [1, [2, [3, [...]]]] print:{'a1': 1, 'b1': 2, 'dic': {'a2': 1, 'b2': 2, 'dic': {'a3': 1, 'b3': 2, 'dic': {'a4': 1, 'b4': 2}}}} pprint.pprint: {'a1': 1,'b1': 2,'dic': {'a2': 1,'b2': 2,'dic': {'a3': 1,'b3': 2,'dic': {...}}}}
6、np数组的美化输出
import numpy as np
ss=np.random.randint(0,5,size=(2,2,2,2,2))
ss=np.random.uniform(-1,1,size=(2,2,2,2,2))
ss=np.around(ss,3)#只保留数据的前三位小数
print("np数组原始输出:\n",ss)
#直接传入np数组是无法有效控制打印数据的格式的,因为print函数会调用numpy对象内置的str方法,把数组转化为特定格式的字符串
print("pprint美化输出1:")
pprint.pprint(ss.tolist())
print("pprint美化输出2: , depth=3")
pprint.pprint(ss.tolist(), depth=3)
输出信息如下所示: np数组原始输出:[[[[[ 0.679 -0.762][ 0.411 -0.892]][[ 0.505 0.286][-0.132 -0.755]]][[[ 0.384 0.995][ 0.254 0.074]][[ 0.562 -0.683][-0.112 -0.027]]]][[[[-0.522 -0.017][ 0.881 0.876]][[ 0.444 -0.963][-0.391 0.438]]][[[ 0.057 -0.593][ 0.853 -0.642]][[-0.933 -0.67 ][-0.671 -0.632]]]]] pprint美化输出1: [[[[[0.679, -0.762], [0.411, -0.892]], [[0.505, 0.286], [-0.132, -0.755]]],[[[0.384, 0.995], [0.254, 0.074]], [[0.562, -0.683], [-0.112, -0.027]]]],[[[[-0.522, -0.017], [0.881, 0.876]], [[0.444, -0.963], [-0.391, 0.438]]],[[[0.057, -0.593], [0.853, -0.642]], [[-0.933, -0.67], [-0.671, -0.632]]]]] pprint美化输出2: , depth=3 [[[[...], [...]], [[...], [...]]], [[[...], [...]], [[...], [...]]]]
使用pprint.pprint()对于np数组无法保证小数点位对齐,使用以下语句可以使小数点位的基本对齐
print(pprint.pformat(ss.tolist()))
输出信息如下所示: [[[[[-0.119, 0.725], [-0.73, -0.262]], [[-0.221, 0.861], [-0.012, -0.479]]],[[[-0.916, 0.529], [-0.885, 0.773]], [[0.219, 0.698], [0.499, -0.95]]]],[[[[-0.268, 0.62], [-0.757, -0.81]], [[0.675, 0.008], [-0.911, -0.225]]],[[[-0.385, 0.891], [0.29, 0.783]], [[-0.761, -0.411], [0.859, 0.536]]]]]
想要更加优美的对齐效果,可以自行对字符串添加空格填充,具体操作如所示
ss=np.random.uniform(-1,1,size=(2,2,2,2,2))
ss=np.around(ss,2)#只保留数据的前两位小数
strs=pprint.pformat(ss.tolist())
strs=strs.replace('[0','[ 0').replace(' -','-')
print(strs)
输出信息如下所示: [[[[[-0.56,-0.04], [-0.35,-0.64]], [[ 0.67,-0.45], [ 0.27, 0.8]]],[[[ 0.93, 0.21], [ 0.77,-0.66]], [[-0.37,-0.45], [ 0.44,-0.55]]]],[[[[ 0.63, 0.09], [ 0.98, 0.11]], [[-0.13, 0.09], [-0.92,-0.92]]],[[[ 0.7,-0.81], [-0.54, 0.24]], [[-0.14, 0.93], [ 0.84, 0.0]]]]]
对于非numpy数组的输出精度控制,可以参考python工具方法 9 控制print中输出任意数据的精度,支持numpy数据、tuple、list、dict、set_a486259的博客-CSDN博客_python 打印列表 控制精度
使用上文的方法进行转换后,可以用pprint进行输出
更优雅的字符串print——pprint库的使用相关推荐
- android string.join java8_Java8 - 更优雅的字符串连接(join)收集器 Collectors.joining
Java8 - 更优雅的字符串连接(join)收集器 Collectors.joining Zebe 2018-10-15 38 0 Java,Java8 StringBuilder,Collecto ...
- python 优雅退出_Python学习教程:Python 使用 backoff 更优雅的实现轮询
我们经常在开发中会遇到这样一种场景,即轮循操作.今天介绍一个Python库,用于更方便的达到轮循的效果--backoff. Python学习教程:Python 使用 backoff 更优雅的实现轮询 ...
- 如何更优雅地对接第三方API
如何更优雅地对接第三方API 本文所有示例完整代码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/ ...
- 9条消除if...else的锦囊妙计,助你写出更优雅的代码
点击上方蓝色"方志朋",选择"设为星标"回复"666"获取独家整理的学习资料! 前言 最近在做代码重构,发现了很多代码的烂味道.其他的不多说 ...
- c语言c2182是什么错误,C语言中一种更优雅的异常处理机制
上一篇文章对C语言中的goto语句进行了较深入的阐述,实际上goto语句是面向过程与面向结构化程序语言中,进行异常处理编程的最原始的支持形式.后来为了更好地.更方便地支持异常处理编程机制,使得程序员在 ...
- python写出的程序如何给别人使用-涨姿势!这些小技巧让小白也可以写出更优雅的Python代码!...
原标题:涨姿势!这些小技巧让小白也可以写出更优雅的Python代码! 一.前言 我前两天回答了两个Python相关的问题,收到了很多赞,从答案被收藏的情况来看,确实对不少人都很有帮助,所以我也很开心. ...
- Python代码如何写的更优雅
首先最重要的一点, 忘掉其他语言里的写法, 尝试使用Python风格进行code, 熟练之后,你会觉得她真的很美! 1. 多个值进行初始化 # > yes s1,s2,s3 = [],[],0 ...
- JavaScript复杂判断的更优雅写法
前提 我们编写js代码时经常遇到复杂逻辑判断的情况,通常大家可以用if/else或者switch来实现多个条件判断,但这样会有个问题,随着逻辑复杂度的增加,代码中的if/else/switch会变得越 ...
- JavaScript 复杂判断的更优雅写法
我们编写js代码时经常遇到复杂逻辑判断的情况,通常大家可以用if/else或者switch来实现多个条件判断,但这样会有个问题,随着逻辑复杂度的增加,代码中的if/else/switch会变得越来越臃 ...
最新文章
- Java中几种常见的循环
- C#LeetCode刷题之#169-求众数(Majority Element)
- sklearn+gensim︱jieba分词、词袋doc2bow、TfidfVectorizer
- Microsoft Visual Studio Team Foundation Server Express 2013 (一) 服务器端安装和配置
- linux5.4获取root权限,gcc++漏洞 普通用户获取root权限
- 浅谈互联网寒冬与经济形势
- 古文观止卷七_陈情表_李密
- 乔伊·伯纳尔(Joey Bernal)的评论专栏,社交网络的三阶段路线图
- python函数进阶小结_python函数的进阶
- android adapter 组件,Android UI - AdapterView 及其子类
- ArcGIS矢量数据模型
- java 国际化_Java国际化基础
- Firefox/Chrome渗透测试插件推荐
- 2019TFE计算机科学排名,2019TFE Times 硕士专业排名
- C语言——PAT 乙级(1002.读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。)
- 基于Ubuntu+Bochs模拟器实现的操作系统图形化的小游戏(2048、flappybird、)
- python怎么处理通达信ctp接口数据?
- JavaScript富应用MVC MVVM框架
- MySQL 并集、交集、差集
- 论文阅读笔记——Vulnerability Dataset Construction Methods Applied To Vulnerability Detection A Survey