On Feb 5, 4:05 pm, marduk <mar...@python.net> wrote: > > Although that implementation also scans the string 10 times (s.count()), > which may not be as efficient (although it is happening in C, so perhaps > not). > > A better solution involves only scanning the string once.
agreed. i was specifically showing how to reverse the loop. using the much-better-suited Counter class: from collections import Counter s=input("Enter a string, eg(4856w23874): ") checkS=['0','1','2','3','4','5','6','7','8','9'] cnt = Counter() for char in s: cnt[char] += 1 for char, tally in sorted(cnt.items()): if char in checkS and tally > 0: if tally == 1: print(char,"occurs 1 time.") else: print(char, "occurs", tally,"times.") >>> Enter a string, eg(4856w23874): 192398209asdfbc12903348955 0 occurs 2 times. 1 occurs 2 times. 2 occurs 3 times. 3 occurs 3 times. 4 occurs 1 time. 5 occurs 2 times. 8 occurs 2 times. 9 occurs 5 times. >>> HTH, Don -- http://mail.python.org/mailman/listinfo/python-list