Make a program that reads some text and produces as output the distribution of words that start with dierent letters. I recently wrote a program that would count how many times a certain letter would show up, but I am not sure how to look to see at the first letter of each word and count that up. I am stumped. My code for the word counting is below, very basic, new to python.

inn = 'Super man was here.'
place = {}
for r in inn:
    place[r] = place.get(r,0) + 1

print place

So the user would do this:

Please type your text: Today is tomorrow.

Would print something out like this:

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]

I got this code so far, but not sure how to count it and what not.

s = "We are not going here tonight." 
def firstLetter(s):
    for item in s.split():
        print item[0].upper()
print firstLetter(s)

Edited by MegaMan15: n/a

4 Years
Discussion Span
Last Post by pyTony

If we mix together what you have it can look like this.

def firstLetter(s):
    '''Count first letter in a sentence'''
    place = {}
    for item in s.lower().split():
        place[item[0]] = place.get(item[0], 0) + 1
    return place

s = "Today is tomorrow"
print firstLetter(s)
#--> {'i': 1, 't': 2}
help(firstLetter) #Look at this to se docstring work

A little more you can look at.

>>> s = "Today is tomorrow"
>>> [lett[0] for lett in s.lower().split()]
['t', 'i', 't']

list comprehension as i use here is much used in python.
As you a simpe way to get first letter.
From python 2.7--> we can use Collections.counter to count stuff.

>>> from collections import Counter
>>> s = "Today is tomorrow"
>>> Counter(lett[0] for lett in s.lower().split())
Counter({'t': 2, 'i': 1})

Edited by snippsat: n/a


Yeah I understand how to do that now thanks. Kind of had that working, but I was wondering how I could print it as I stated, showing that there are zero of whatever letters aren't used and show the numbers for the letters that are shown.


Let's assume you want something like this ...

import pprint

# create a dictionary of keys a to z and zero values
d = dict([(chr(k), 0) for k in range(ord('a'),ord('z'))])

#pprint.pprint(d)  # test

# the test sentence
s = "Albert has many marvelous attributes"

# convert to all lower characters
s = s.lower()

# split the sentence into words and iterate
for word in s.split():
    # use the starting letter of each word
    letter = word[0]
    # if the letter is present increment the dictionary value
    d[letter] = d.get(letter, 0) + 1

# pretty print the result

''' output >>
{'a': 2,
 'b': 0,
 'c': 0,
 'd': 0,
 'e': 0,
 'f': 0,
 'g': 0,
 'h': 1,
 'i': 0,
 'j': 0,
 'k': 0,
 'l': 0,
 'm': 2,
 'n': 0,
 'o': 0,
 'p': 0,
 'q': 0,
 'r': 0,
 's': 0,
 't': 0,
 'u': 0,
 'v': 0,
 'w': 0,
 'x': 0,
 'y': 0}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.