zoukankan      html  css  js  c++  java
  • Python学习笔记字典之漂亮打印pprint模块

    随笔记录方便自己和同路人查阅。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      如果程序中倒入pprint模块,就可以使用pprint()和pformat()函数,它们将“漂亮打印”一个字典的字。如果

    想要字典中表项的现实比print()的输出结果更干净,这就游泳了。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      1、普通打印,示例代码1:

    count = {}
    message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
    for character in message:
        count.setdefault(character,0)
        count[character] = count[character] +1
    print(count)
    

      运行结果:

      2、漂亮打印,示例代码:

    import pprint
    count = {}
    message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
    for character in message:
        count.setdefault(character,0)
        count[character] = count[character] +1
    pprint.pprint(count)
    

      运行结果:

      3、如果希望得到漂亮打印的文本作为字符串,而不是显示在屏幕上,那就调用pprint.format(),示例代码:

    import pprint
    count = {}
    message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
    for character in message:
        count.setdefault(character,0)
        count[character] = count[character] +1
    #pprint.pprint(count)
    print(pprint.pformat(count))
    

      运行结果:

      根据运行结果可以看出,pprint.pprint(count)和print(pprint.pformat(count))是等价的

  • 相关阅读:
    设计模式小结
    Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一)
    全新对待.net一次全面的旅程
    页面生命周期小结
    面向对象点滴
    Chapter 2.1:WCF服务契约的重载与继承详解
    一封给“X教授”的回信(讨论Socket通信)
    Chapter 1.4:WCF实践 元数据详解
    有了WCF,Socket是否已人老珠黄?
    Chapter 1.3:WCF实践 HelloWorld
  • 原文地址:https://www.cnblogs.com/lirongyang/p/9544706.html
Copyright © 2011-2022 走看看