2

I need a function that counts how many times each individual letter appears in a string. It has to count capitals and lowercase as the same letter. I sort of did it, but its not exactly pretty.

def lmao(x):

    aye=x.count("a")
    Aye=x.count("A")
    bye=x.count("b")
    Bye=x.count("B")
    cye=x.count("c")
    Cye=x.count("C")
    dye=x.count("d")
    Dye=x.count("D")
    Eye=x.count("E")
    eye=x.count("e")
    Fye=x.count("F")
    fye=x.count("f")
    Gye=x.count("G")
    gye=x.count("g")
    Hye=x.count("H")
    hye=x.count("h")
    Iye=x.count("I")
    iye=x.count("i")
    Jye=x.count("J")
    jye=x.count("j")
    Kye=x.count("K")
    kye=x.count("k")
    Lye=x.count("L")
    lye=x.count("l")
    Mye=x.count("M")
    mye=x.count("m")
    Nye=x.count("N")
    nye=x.count("n")
    Oye=x.count("O")
    oye=x.count("o")
    Pye=x.count("P")
    pye=x.count("P")
    Qye=x.count("Q")
    qye=x.count("q")
    rye=x.count("r")
    Rye=x.count("R")
    sye=x.count("s")
    Sye=x.count("S")
    tye=x.count("t")
    Tye=x.count("T")
    uye=x.count("u")
    Uye=x.count("U")
    Vye=x.count("V")
    vye=x.count("v")
    Wye=x.count("W")
    wye=x.count("w")
    Xye=x.count("X")
    xye=x.count("x")
    Yye=x.count("Y")
    yye=x.count("y")
    Zye=x.count("Z")
    zye=x.count("z")
    killme=(aye+Aye,bye+Bye,cye+Cye,Dye+dye,Eye+eye,Fye+fye,Gye+gye,Hye+hye,Iye+iye,jye+Jye,Kye+kye,Lye+lye,Mye+mye,Nye+nye,Oye+oye,Pye+pye,Qye+qye,rye+Rye,sye+Sye,Tye+tye,uye+Uye,Vye+vye,Wye+wye,xye+Xye,Yye+yye,Zye+zye)
    return killme

So yeah, thats the disaster that I came up with. Is there any way to shorten this process?

midori
  • 4,807
  • 5
  • 34
  • 62
A.raven
  • 105
  • 3
  • 9
  • I like the name of your final variable. Here's some related read [Count occurrence of a character in a string](http://stackoverflow.com/q/1155617) and [Alphabet range python](http://stackoverflow.com/q/16060899) – Bhargav Rao Feb 11 '16 at 20:48
  • Could use a dict and just use each letter you find as a key and use the value for a count of how many you seen. Then add up all values in dict. – Jacobr365 Feb 11 '16 at 20:50

4 Answers4

5

Use collections.Counter https://docs.python.org/2/library/collections.html#counter-objects

from collections import Counter    
counter = Counter(mystr.lower())

will give you all occurrences of each letter, ignoring case

limasxgoesto0
  • 4,555
  • 8
  • 31
  • 38
  • I suppose he wants the count of letters that are not in the string as well. – Vincent Savard Feb 11 '16 at 20:53
  • @VincentSavard that's no problem, the counter returns 0 by default when it does not find a key. – timgeb Feb 11 '16 at 20:55
  • @timgeb: I meant in the return tuple. – Vincent Savard Feb 11 '16 at 20:55
  • @VincentSavard: True. It's possible to generate all letters using ord() and range() as per http://stackoverflow.com/questions/7001144/range-over-character-in-python. OP can use this to see what's not in the counter – limasxgoesto0 Feb 11 '16 at 20:56
  • @TomNash: the OP said "it has to count capitals and lowercase as the same letter" – limasxgoesto0 Feb 11 '16 at 20:56
  • You can always `return tuple(counter[c] for c in string.lowercase)` to return an equivalent tuple as the OP, and as @timgeb pointed out `0` would be in place for any non-existent character. – AChampion Feb 11 '16 at 21:12
2

To return exactly what you've requested:

import string
def lmao(x):
    return tuple(x.lower().count(c) for c in string.lowercase)
Brian
  • 2,172
  • 14
  • 24
0

This may help:

def counter(text):
    count_list = []
    for char in "abcdefghijklmnopqrstuvwxyz":
        count_list.append(text.lower().count(char))
    return tuple(count_list)

print(counter("Helllo123"))

Output:

(0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
0

Use counter together with a dictionary comprehension to get the count of each letter.

import string
from collections import Counter

For an alpha sorted tuple,

def count_letter_tuples(sentence):
    return tuple(Counter(sentence.lower()).get(c, 0) for c in string.ascii_lowercase)

>>> count_letter_tuples("Some Big Sentence")
(0, 0, 1, 0, 4, 0, 1, 0, 1, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)

Or for a dictionary response:

def count_letters(sentence):
    return {c: Counter(sentence.lower()).get(c, 0) for c in string.ascii_lowercase}

>>> count_letters("Some Big Sentence")
{'a': 0,
 'b': 1,
 'c': 1,
 'd': 0,
 'e': 4,
 'f': 0,
 'g': 1,
 'h': 0,
 'i': 1,
 'j': 0,
 'k': 0,
 'l': 0,
 'm': 1,
 'n': 2,
 'o': 1,
 'p': 0,
 'q': 0,
 'r': 0,
 's': 2,
 't': 1,
 'u': 0,
 'v': 0,
 'w': 0,
 'x': 0,
 'y': 0,
 'z': 0}
Alexander
  • 105,104
  • 32
  • 201
  • 196