I would use the collections.Counter() object to collect the lengths and then accumulate the amounts:
from collections import Counter lengths = Counter(len(v) for v in userIdDict.values()) total = 0 accumulated = {} for length in range(max(lengths), -1, -1): count = lengths.get(length, 0) total += count accumulated[length] = total
Thus, it collects counts for each length, and then creates a dictionary with cumulative length. This is an O (N) algorithm; you repeat all the values ββonce, then add a few smaller direct loops (for max() and the accumulation loop):
>>> from collections import Counter >>> import random >>> testdata = {''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(5)): [None] * random.randint(1, 10) for _ in range(100)} >>> lengths = Counter(len(v) for v in testdata.values()) >>> lengths Counter({8: 14, 7: 13, 2: 11, 3: 10, 4: 9, 5: 9, 9: 9, 10: 9, 1: 8, 6: 8}) >>> total = 0 >>> accumulated = {} >>> for length in range(max(lengths), -1, -1): ... count = lengths.get(length, 0) ... total += count ... accumulated[length] = total ... >>> accumulated {0: 100, 1: 100, 2: 92, 3: 81, 4: 71, 5: 62, 6: 53, 7: 45, 8: 32, 9: 18, 10: 9}
source share