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)

Recommended Answers

All 4 Replies

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})

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
pprint.pprint(d)

''' 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}

'''

@vegaseat: that infamous +1 again. We need space for Zorro also, don't we.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.