0

so far this is what i have and this and this will just print out a list with the count of each letter in the string. it only checks for lowercase letters.

`S="""Four score and seven years ago our fathers brought forth on this continent a new nation
Now we are engaged in a great civil war
"""
lowlet = S.lower()
L_count = [0]*26
total_count = 0
alpha = "abcdefghijklmnopqrstuvwxyz"
i = 0
while i < len(lowlet):
    if lowlet[i] in alpha:
        L_count[i][ord(lowlet[i]) - 97] += 1
        total_count += 1
    i += 1
print('total count of letters:',total_count)'

now im giving this algorithm but i cant put it into code and i cant use a for loop i have to use a while loop Initialize a list, L_freq. For each element, count, in L_counts Find the letter corresponding to this count Insert in L_freq, the list: [count, letter]

Mike
  • 53
  • 1
  • 7
  • here are [multiple ways to count frequencies of characters in a text](http://stackoverflow.com/a/2525617/4279) – jfs Oct 14 '14 at 01:48

1 Answers1

0

is it a requirement that it be a list? I feel like a dictionary would be easier to handle

sentence = s.lower()
counts = { letter: sentence.count(letter) for letter in alpha }
print(counts)

this will print like: {'a': 5, 'b': 2}

corvid
  • 10,733
  • 11
  • 61
  • 130
  • yes its a requirement what i was thinking was a tuple in a list. yea most diffidently a dictionary would be easier. Thats why im having so much trouble with all this restrictions. – Mike Oct 14 '14 at 01:27
  • Have you considered using a `collections.Counter`? – corvid Oct 14 '14 at 01:39
  • i haven't actually got to that topic yet but i just looked them up and it seems like a good idea. – Mike Oct 14 '14 at 01:52