> Pythons 2.7 and later have dictionary comprehensions. So you can do > this: > > >>> {item: s.count(item) for item in set(s)} > > {'a': 1, 'b': 1, '1': 2, '3': 1, '2': 2, '4': 1} > > Which gives counts for all letters. To filter out the digit-counts > only: > > >>> digits="0123456789" > >>> {item: s.count(item) for item in set(s) if item in dig} > > {'1': 2, '3': 1, '2': 2, '4': 1} > > You can then print out the values in d in any which way you want. > [Starting with printing is usually a bad idea]
Sorry cut-paste slip-up. 1. use dig or digits (or none, just inline the "0123456789") 2. I assumed >>> s = "12ab3412" -- http://mail.python.org/mailman/listinfo/python-list