I need to count the number of identical rows running. For this, I use itertools.groupby .

Question: how to determine the number of elements in a group? len (grop) does not work. Does the object have any property like size ()?

data = (line.rstrip() for line in sys.stdin) for key, group in groupby(data): print(data.size()) 

1 answer 1

 import itertools qwe = 'qwe2', 'qwe2', 'qwe1', 'qwe2' for key, group in itertools.groupby(qwe): li = list(group) print('key {} has len {}: {}'.format(key, len(li), li)) 

out:

 key qwe2 has len 2: ['qwe2', 'qwe2'] key qwe1 has len 1: ['qwe1'] key qwe2 has len 1: ['qwe2'] 
  • one
    If a group can be very large (we do not know what is fed in stdin), then you can use sum(1 for _ in group) (assuming that we are not interested in anything but a number). sum(..) may be slower than len(list(group)) , but it allows at least terabytes of data to be fed. - jfs