-1

I have a numerical list :

numlist = [601, 601, 601, 602, 602, 603, 1245, 1245, 1245, 1245, 1247, 1249, 1250,602,602]

This list will always be "sorted" like shown, with values restarting after a certain point.

In that list, I need to group and count the number of identical values, and add to the count the next item IF it's numerical value is one (and only one) more than the preceding item.

Expected result :

>>>result
[6,4,1,2,2]

I can do :

>>>from itertools import groupby
>>>[len(list(group)) for key, group in groupby(numlist)]
[3, 2, 1, 4, 1, 1, 1, 2]

which count the number of identical values, but how to also include in the groups values as described above ?

More explanation :

Go through the list, if next item is the same value or same value +1, group them.

Chargaff
  • 1,562
  • 2
  • 19
  • 41

1 Answers1

3

Apply itertools.groupby twice:

from itertools import groupby

numlist = [601, 601, 601, 602, 602, 603, 1245, 1245, 1245, 1245, 1247, 1249, 1250,602,602]
group_first = ((k, sum(1 for _ in g)) for k, g in groupby(numlist))

Here after the first grouping we will have something like:

[(601, 3), (602, 2), (603, 1), (1245, 4), (1247, 1), (1249, 1), (1250, 1), (602, 2)]

Now we need to group these items again(by first item), but the trick here is to use enumerate to group consecutive items. The difference of such items with their corresponding indices is always going to be same(Oh! and BTW I learned that from Python 2.6's itertools documentation;-)).

for k, g in groupby(enumerate(group_first), lambda (i, x):i-x[0]):
    print sum(v for i, (k, v) in g)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504