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')

 

posted @ 2014-09-01 20:56  牛而立  阅读(223)  评论(0编辑  收藏  举报