• Python深入学习笔记(二)


    计数器Counter

    Counter类是自Python2.7起增加的,属于字典类的子类,是一个容器对象,主要用来统计散列对象,支持集合操作+、-、&、|,其中后两项分别返回两个Counter对象各元素的最小值和最大值。

    >>> from collections import Counter
    >>> c = Counter('success')
    >>> c
    Counter({'s': 3, 'c': 2, 'e': 1, 'u': 1})
    >>> c.most_common(2)
    [('s', 3), ('c', 2)]
    >>> c.update('successfully')
    >>> c
    Counter({'s': 6, 'c': 4, 'u': 3, 'e': 2, 'l': 2, 'f': 1, 'y': 1})
    >>> c.subtract('success')
    >>> c
    Counter({'s': 3, 'c': 2, 'l': 2, 'u': 2, 'e': 1, 'f': 1, 'y': 1})

    读取配置文件ConfigParser

    # 配置文件config.conf
    [DEFAULT]
    conn_str = %(host)s:%(port)s/%(path)s
    
    [conn1]
    host = localhost
    port = 80
    path = index
    
    [conn2]
    host = 10.0.1.1
    port = 8080
    path = admin
    
    # readconfig.py
    import ConfigParser
    
    conf = ConfigParse.ConfigParser()
    conf.read('config.conf')
    print conf.get('conn1', 'conn_str')
    print conf.get('conn2', 'conn_str')
  • 相关阅读:
    使用telnet模拟http请求
    07_Python变量内存地址、小数据池
    04_Linux命令
    03_Linux文件和目录
    06_Python Encoded
    05_Python Format Operation
    04_Python Data Structures
    02_Python基本数据类型
    01_软件开发流程
    03_线性表应用一:栈
  • 原文地址:https://www.cnblogs.com/newerly/p/3950011.html
Copyright © 2020-2023  润新知