Python groupby 用法
Tags: python
例子
from itertools import groupby
lst = [5, 4, 4, 4, 5, 5, 2, 5]
for n, l in groupby(lst):
print n, list(l)
得到
5 [5]
4 [4, 4, 4]
5 [5, 5]
2 [2]
5 [5]
如果
for n, l in groupby(sorted(lst)):
print n, list(l)
得到
2 [2]
4 [4, 4, 4]
5 [5, 5, 5, 5]
Read more: