This solution takes advantage of the fact that your elements are already ordered, and only makes a single pass through your original list (and a constant number of passes through the remaining data structures.
>>> from itertools import groupby
>>> x = [2345,67,24,24,11,11,6,6,6,6,6,3,3,3,3,3,1,1,1,1,1,1]
>>> grouped_x = [(k, sum(1 for i in g)) for k,g in groupby(x)]
>>> grouped_x
[(2345, 1), (67, 1), (24, 2), (11, 2), (6, 5), (3, 5), (1, 6)]
The groupby
expression is borrowed from the first question I ever asked on SO, and basically just groups each contiguous block of the same value into a list
of (value, instance generator) pairs. Then the outer list comprehension just converts the instance generators into their total length.
I think OP's figures were not accurate, but this seems to be something like what he was getting at. I took the ceiling function, but you could also round
>>> from math import ceil
>>> for k, v in grouped_x:
print int(ceil(100 * v / float(len(x)))),
print "% is", k
5 % is 2345
5 % is 67
10 % is 24
10 % is 11
23 % is 6
23 % is 3
28 % is 1