Pergunta

I've done this but It's too long, how can I do a much simpler way? Thanks in advance

letter_a = all_words.count('a')

letter_b = all_words.count('b')

letter_c = all_words.count('c')

letter_d = all_words.count('d')

letter_e = all_words.count('e')

letter_f = all_words.count('f')

letter_g = all_words.count('g')

letter_h = all_words.count('h')

letter_i = all_words.count('i')

letter_j = all_words.count('j')

letter_k = all_words.count('k')

letter_l = all_words.count('l')

letter_m = all_words.count('m')

letter_n = all_words.count('n')

letter_o = all_words.count('o')

letter_p = all_words.count('p')

letter_q = all_words.count('q')

letter_r = all_words.count('r')

letter_s = all_words.count('s')

letter_t = all_words.count('t')

letter_u = all_words.count('u')

letter_v = all_words.count('v')

letter_w = all_words.count('w')

letter_x = all_words.count('x')

letter_y = all_words.count('y')

letter_z = all_words.count('z')



print("There is:\n"


 "A:",letter_a,",\n"

  "B:",letter_b,",\n"

  "C:",letter_c,",\n"

  "D:",letter_d,",\n"

  "E:",letter_e,",\n"

  "F:",letter_f,",\n"

  "G:",letter_g,",\n"

  "H:",letter_h,",\n"

  "I:",letter_i,",\n"

  "J:",letter_j,",\n"

  "K:",letter_k,",\n"


  "L:",letter_l,",\n"

  "M:",letter_m,",\n"

  "N:",letter_n,",\n"

  "O:",letter_o,",\n"

  "P:",letter_p,",\n"

  "Q:",letter_q,",\n"

  "R:",letter_r,",\n"

  "S:",letter_s,",\n"

  "T:",letter_t,",\n"

  "U:",letter_u,",\n"


  "V:",letter_v,",\n"

  "W:",letter_w,",\n" 

  "X:",letter_x,",\n"

  "Y:",letter_y,",\n"

  "Z:",letter_z,

  "\n")
Foi útil?

Solução 2

Definitely you can reduce the code to pretty much two lines. But readability might be an issue if you don't know python syntax

import string
all_words = 'this is me'
print("there is:\n {0}".format('\n'.join([letter+':'+str(all_words.count(letter) + all_words.count(letter.lower())) for letter in string.uppercase])))

Outras dicas

There are various answers - certainly, as you wrote out letter_X = all_words.count('X') for the tenth time you should have been thinking "perhaps a for loop would save me from this?" And it would:

import string  

for character in string.ascii_lowercase:
    ...

Similarly:

  • "Rather than lots of separate variables, could I use a dict with the letter as the key and the count as a value?"
  • "Do I need to store all of these, then print them, or could I just print them straight away?"

However, the simplest thing to do here is use collections.Counter, for example:

>>> from collections import Counter
>>> counter = Counter("foo bar baz")
>>> counter
Counter({'a': 2, ' ': 2, 'b': 2, 'o': 2, 'f': 1, 'r': 1, 'z': 1})
>>> counter['a']
2
>>> counter['c']
0

This way you only process the string once, instead of counting for every letter. Counter is basically a dictionary with some extra useful features.

Also, you need to consider case - should "A" count as "a", or vice versa, or are they separate?

Use for loops. An example for a for loop that would count the letters 'a' and 'b':

for character in "ab":
    print(character + " has " + str(all_words.count(character)) + " occurences.")

If you don't want to user collection.Counter and other libraries (such as string.ascii_uppercase) and build your own algorithm, you can try this:

all_words = 'asasasaassasaasasasasassa'
upper_words = all_words.upper()
letter_freq = {}
for letter in set(upper_words):
    l etter_freq[letter] = upper_words.count(letter)

for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    print '%s: %d'%(letter, letter_freq.get(letter, 0))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top