分组

 

 

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

 

你可能感兴趣的:(分组)