http://blog.csdn.net/pipisorry/article/details/46947833
集合库collection
collections模块介绍
Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:
1.namedtuple(): 生成可以使用名字来访问元素内容的tuple子类
2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
3.Counter: 计数器,主要用来计数
4.OrderedDict: 有序字典
5.defaultdict: 带有默认值的字典
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(x=1.0, y=2.0)
>>> p
Point(x=1.0, y=2.0)
>>> p.x
1.0
>>> p.y
2.0
继承命名tuples
deque双端队列
deque其实是 double-ended queue 的缩写,翻译过来就是双端队列,它最大的好处就是实现了从队列 头部快速增加和取出对象: .popleft(), .appendleft() 。
你可能会说,原生的list也可以从头部添加和取出对象啊?就像这样:
l.insert(0, v)
l.pop(0)
list对象的这两种用法的时间复杂度是 O(n) ,也就是说随着元素数量的增加耗时呈 线性上升。而使用deque对象则是 O(1) 的复杂度,所以当你的代码有这样的需求的时候, 一定要记得使用deque。
作为一个双端队列,deque还提供了一些其他的好用方法,比如 rotate 等。
举个栗子
使用deque的rotate方法来实现了一个无限循环的加载动画
import sys
import time
from collections import deque
fancy_loading = deque('>--------------------')
while True:
print '\r%s' % ''.join(fancy_loading),
fancy_loading.rotate(1)
sys.stdout.flush()
time.sleep(0.08)
# Result:
# 一个无尽循环的跑马灯
------------->-------
双向队列
操作
>>> Q
=
collections.deque()
>>> Q.append(
1
)
>>> Q.appendleft(
2
)
>>> Q.extend([
3
,
4
])
>>> Q.extendleft([
5
,
6
])
>>> Q
deque([
6
,
5
,
2
,
1
,
3
,
4
])
>>> Q.pop()
4
>>> Q.popleft()
6
>>> Q
deque([
5
,
2
,
1
,
3
])
>>> Q.rotate(
3
)
>>> Q
deque([
2
,
1
,
3
,
5
])
>>> Q.rotate(
-
3
)
>>> Q
deque([
5
,
2
,
1
,
3
])
限制长度的双向队列
>>> last_three
=
collections.deque(maxlen
=
3
)
>>>
for
i
in
xrange
(
10
):
... last_three.append(i)
...
print
", "
.join(
str
(x)
for
x
in
last_three)
...
0
0
,
1
0
,
1
,
2
1
,
2
,
3
2
,
3
,
4
http://blog.csdn.net/pipisorry/article/details/46947833
Counter计数器 - Multisets运算
官方解释
It is kind of redundant, in that Collections already contains a class for doing this, called Counter. In this case, I need to first extract the second item from eachtuple
, for which I can use a generator expression.
|
Another, maybe better, example might be counting all the different lines that appear in a file. It becomes very simple.
|
统计所有字符出现次数
from collections import Counter
s = '''A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.'''.lower()
c = Counter(s)
# 获取出现频率最高的5个字符
print c.most_common(5)
# Result:
[(' ', 54), ('e', 32), ('s', 25), ('a', 24), ('t', 24)]
Multisets运算
>>> A
=
collections.Counter([
1
,
2
,
2
])
>>> B
=
collections.Counter([
2
,
2
,
3
])
>>> A
Counter({
2
:
2
,
1
:
1
})
>>> B
Counter({
2
:
2
,
3
:
1
})
>>> A | B
Counter({
2
:
2
,
1
:
1
,
3
:
1
})
>>> A & B
Counter({
2
:
2
})
>>> A
+
B
Counter({
2
:
4
,
1
:
1
,
3
:
1
})
>>> A
-
B
Counter({
1
:
1
})
>>> B
-
A
Counter({
3
:
1
})
列表中出现最多的元素
>>> A
=
collections.Counter([
1
,
1
,
2
,
2
,
3
,
3
,
3
,
3
,
4
,
5
,
6
,
7
])
>>> A
Counter({
3
:
4
,
1
:
2
,
2
:
2
,
4
:
1
,
5
:
1
,
6
:
1
,
7
:
1
})
>>> A.most_common(
1
)
[(
3
,
4
)]
>>> A.most_common(
3
)
[(
3
,
4
), (
1
,
2
), (
2
,
2
)]
OrderedDict有序字典
在Python中,dict这个数据结构由于hash的特性,是无序的,这在有的时候会给我们带来一些麻烦, 幸运的是,collections模块为我们提供了OrderedDict,当你要获得一个有序的字典对象时,用它就对了。
举个栗子
from collections import OrderedDict
items = (
('A', 1),
('B', 2),
('C', 3)
)
regular_dict = dict(items)
ordered_dict = OrderedDict(items)
print 'Regular Dict:'
for k, v in regular_dict.items():
print k, v
print 'Ordered Dict:'
for k, v in ordered_dict.items():
print k, v
# Result:
Regular Dict:
A 1
C 3
B 2
Ordered Dict:
A 1
B 2
C 3
Note:直接输出的OrderedDict形式如下:print(OrderedDict(reguale_slist))
OrderedDict([('GET', 'http://www.jobbole.com/login/?redirect=http://www.jobbole.com/ HTTP/1.1'), ('Host', 'www.jobbole.com'), ('Connection', 'keep-alive'), ('Cookie', 'wordpress_test_cookie=WP+Cookie+check')...])
而不是dict的形式:print(dict(reguale_slist))
{'Referer': 'http://www.jobbole.com/', 'Host': 'www.jobbole.com', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate, sdch', ...}
>>> m
=
dict
((
str
(x), x)
for
x
in
range
(
10
))
>>>
print
", "
.join(m.keys())
1
,
0
,
3
,
2
,
5
,
4
,
7
,
6
,
9
,
8
>>> m
=
collections.OrderedDict((
str
(x), x)
for
x
in
range
(
10
))
>>>
print
", "
.join(m.keys())
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
>>> m
=
collections.OrderedDict((
str
(x), x)
for
x
in
range
(
10
,
0
,
-
1
))
>>>
print
", "
.join(m.keys())
10
,
9
,
8
,
7
,
6
,
5
,
4
,
3
,
2
,
1
defaultdict默认字典
我们都知道,在使用Python原生的数据结构dict的时候,如果用 d[key] 这样的方式访问, 当指定的key不存在时,是会抛出KeyError异常的。
但是,如果使用defaultdict,只要你传入一个默认的工厂方法,那么请求一个不存在的key时, 便会调用这个工厂方法使用其结果来作为这个key的默认值。
举个栗子
from collections import defaultdict
members = [
# Age, name
['male', 'John'],
['male', 'Jack'],
['female', 'Lily'],
['male', 'Pony'],
['female', 'Lucy'],
]
result = defaultdict(list)
for sex, name in members:
result[sex].append(name)
print result
# Result:
defaultdict(<type 'list'>, {'male': ['John', 'Jack', 'Pony'], 'female': ['Lily', 'Lucy']})
[Python collections模块实例]
常用操作
>>> m
=
dict
()
>>> m[
"a"
]
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
KeyError:
"a"
>>>
>>> m
=
collections.defaultdict(
int
)
>>> m[
"a"
]
0
>>> m[
"b"
]
0
>>> m
=
collections.defaultdict(
str
)
>>> m[
"a"
]
""
>>> m[
"b"
]
+
=
"a"
>>> m[
"b"
]
"a"
>>> m
=
collections.defaultdict(
lambda
:
"[default value]"
)
>>> m[
"a"
]
"[default value]"
>>> m[
"b"
]
"[default value]"
使用defaultdict代表tree
>>>
import
json
>>> tree
=
lambda
: collections.defaultdict(tree)
>>> root
=
tree()
>>> root[
"menu"
][
"id"
]
=
"file"
>>> root[
"menu"
][
"value"
]
=
"File"
>>> root[
"menu"
][
"menuitems"
][
"new"
][
"value"
]
=
"New"
>>> root[
"menu"
][
"menuitems"
][
"new"
][
"onclick"
]
=
"new();"
>>> root[
"menu"
][
"menuitems"
][
"open"
][
"value"
]
=
"Open"
>>> root[
"menu"
][
"menuitems"
][
"open"
][
"onclick"
]
=
"open();"
>>> root[
"menu"
][
"menuitems"
][
"close"
][
"value"
]
=
"Close"
>>> root[
"menu"
][
"menuitems"
][
"close"
][
"onclick"
]
=
"close();"
>>>
print
json.dumps(root, sort_keys
=
True
, indent
=
4
, separators
=
(
","
,
": "
))
{
"menu"
: {
"id"
:
"file"
,
"menuitems"
: {
"close"
: {
"onclick"
:
"close();"
,
"value"
:
"Close"
},
"new"
: {
"onclick"
:
"new();"
,
"value"
:
"New"
},
"open"
: {
"onclick"
:
"open();"
,
"value"
:
"Open"
}
},
"value"
:
"File"
}
}
[查看更多]
映射对象到唯一的计数数字
>>>
import
itertools, collections
>>> value_to_numeric_map
=
collections.defaultdict(itertools.count().
next
)
>>> value_to_numeric_map[
"a"
]
0
>>> value_to_numeric_map[
"b"
]
1
>>> value_to_numeric_map[
"c"
]
2
>>> value_to_numeric_map[
"a"
]
0
>>> value_to_numeric_map[
"b"
]
1
上面只是非常简单的介绍了一下collections模块的主要内容,主要目的就是当你碰到适合使用 它们的场所时,能够记起并使用它们,起到事半功倍的效果。
如果要对它们有一个更全面和深入了解的话,还是建议阅读官方文档和模块源码。
ref:https://docs.python.org/2/library/collections.html#module-collections
30 Python Language Features and Tricks You May Not Know About