1

So basically I need the program to count how many times each letter is used in a sentence. The only thing I've found is this, and it's ugly:

from collections import Counter
str = "Mary had a little lamb"
counter = Counter(str)
print counter['a']
print counter['b']
print counter['c']
print counter['d']
print counter['e']
print counter['f']
#etc to z

Is there a simpler way to do this????

user3018867
  • 17
  • 1
  • 1
  • 3
  • 1
    The code you mention is actually simpler than what you'd get in most other languages (thanks to the helpful `collections` module). Unless you're wondering how to write a `for` cycle... is that it? – loopbackbee Dec 05 '13 at 18:27
  • Take a look at [this answer](http://stackoverflow.com/a/538374/1454048). You'll need to write the counting code yourself, but that'll get you started. – admdrew Dec 05 '13 at 18:28
  • Not sure which aspect you think is ugly. The counting operation is just two lines (import Counter class, then create Creater instance). Is it the succession of `print` statements that you find ugly? Of course there are a million different ways to use/present the information once you have it. – jez Dec 05 '13 at 18:29

6 Answers6

2

Assuming I understand you:

>>> from collections import Counter
>>> from string import ascii_lowercase
>>> s = "Mary had a little lamb"
>>> counts = Counter(s.lower())
>>> for letter in ascii_lowercase:
...     print letter, counts[letter]
...     
a 4
b 1
c 0
[...]
x 0
y 1
z 0

Note that I lowercased the string so that m would give 2; if you don't want that, remove .lower() and use string.ascii_letters instead.

DSM
  • 342,061
  • 65
  • 592
  • 494
  • 1
    +1 for teaching me that `counts["non-existing key"]` returns `0`. I didn't know that before and would have expected a `KeyError`. – Tim Pietzcker Dec 05 '13 at 18:30
  • @TimPietzcker: yeah, even though `Counter` isn't a subclass of `defaultdict`, it behaves like one in that respect. – DSM Dec 05 '13 at 18:32
1

You just need to loop through all the lowercase letters, and print out the counts for each:

>>> from collections import Counter
>>> sentence = "Mary had a little lamb"
>>> counter = Counter(sentence)
>>> from string import ascii_lowercase
>>> for letter in ascii_lowercase:
...     print '{} = {}'.format(letter, counter[letter])
a = 4
b = 1
c = 0
d = 1
e = 1
f = 0
g = 0
h = 1
i = 1
j = 0
k = 0
l = 3
m = 1
n = 0
o = 0
p = 0
q = 0
r = 1
s = 0
t = 2
u = 0
v = 0
w = 0
x = 0
y = 1
z = 0
mdml
  • 22,442
  • 8
  • 58
  • 66
1

If you only want to print information about the characters that are actually in the string, a simple way would be to do

for key in sorted(counter.keys()):
    print("{0} appears {1} times.".format(key, counter[key]))

Result:

  appears 4 times.
M appears 1 times.
a appears 4 times.
b appears 1 times.
d appears 1 times.
e appears 1 times.
h appears 1 times.
i appears 1 times.
l appears 3 times.
m appears 1 times.
r appears 1 times.
t appears 2 times.
y appears 1 times.

Note that the space character is counted, too.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1
from collections import Counter
str = "Mary had a little lamb"
counter = Counter(str)
for c in set(str):
    print '%s -> %d' % (c, counter[c])
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
sphere
  • 1,330
  • 13
  • 24
0
import string
from collections import Counter
str = "Mary had a little lamb"
counter = Counter(str)
for letter in string.lowercase:
    print counter[letter]
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
0

See this answer, it'll give you an example of how to create a range of characters. Then simply use the foreach loop :

for item in letters_a_to_z:
    print counter[item]
Community
  • 1
  • 1
Danstahr
  • 4,190
  • 22
  • 38