Letter frequency in python

后端 未结 3 1248
南方客
南方客 2020-12-18 13:20

I need to make a program that prints out the frequency of letters in a text file and compares that frequency with that of another in python.

So far I am able to prin

3条回答
  •  星月不相逢
    2020-12-18 13:57

    I think this is a really straight forward way to do it:

    while True:
        speech = raw_input("Enter file name:")
    
        wholeFile = open(speech, 'r+').read()
        lowlet = wholeFile.lower()
    
        alphas = 'abcdefghijklmnopqrstuvwxyz'
    
        # lets set default values first
        occurrences = {letter : 0 for letter in alphas }
        # occurrences = dict(zip(alphas, [0]*len(alphas))) # for python<=2.6
    
        # total number of valid letters
        total = 0
    
        # iter everything in the text
        for letter in lowlet:
            # if it is a valid letter then it is in occurrences
            if letter in occurrences:
                # update counts
                total += 1
                occurrences[letter] += 1
    
        # now print the results:
        for letter, count in occurrences.iteritems():
            print letter, (1.0*count/total)
    

    As you notices you need the total number of valid letters in the text before you can calculate the frequency. Either you filter the text before processing it, or you combine the filtering with the processing, which is what I do here.

提交回复
热议问题