[Tutor] Count Help

Jackie Canales jackiexxduh at yahoo.com
Thu Oct 24 04:12:24 CEST 2013


let say i have a file with random letters of  A, AB, C, CD, AC, A, D, CD, DD, C, B, AB, CD, AB
How do i count the occurrence of each individual item.

def occurence(name):
    infile = open('bloodtype1.txt', 'r')
    lst = infile.read()
    infile.close()

    print(lst.count('AB')) # 3
    print(lst.count('CD')) # 2
    print(lst.count('DD'))# 1


i know i can do lst.count("AB") it will give me 3 but if i were to do lst.count("A") it would give me 6 what I am having trouble with is just getting the occurrence of the A by itself to give me 1 since there is just one occurrence of it and would need to do the same for c and d with out it counting the them in the occurrence of AC and DD or CD.


More information about the Tutor mailing list