分组

 

 

import itertools
for key,group in itertools.groupby('aabbbcmmaaaa'):
   print key,list(group)
返回:
a ['a', 'a']
b ['b', 'b', 'b']
c ['c']
m ['m', 'm']
a ['a', 'a', 'a', 'a']

#忽略大小写:
for key,group in itertools.groupby('AAbbbcMmaaAa',lambda x:x.upper()):
  print key,list(group)
返回:
A ['A', 'A']
B ['b', 'b', 'b']
C ['c']
M ['M', 'm']
A ['a', 'a', 'A', 'a']

 

posted on 2016-01-15 16:34  momingliu11  阅读(184)  评论(0编辑  收藏  举报