For all the elements in syou can do this. In addition, it will find the counts for each element in one pass of the row s, therefore, it will be linear in the number of elements in s.
>>> s = 'ZZAZAAZ'
>>> d = {}
>>> for i, item in enumerate(s):
... d[item] = d.get(item, 0) + i + 1
>>> print d
{'A': 14, 'Z': 14}
source
share