简体   繁体   中英

Listing most common suffixes using Python

I am trying to write a function that will find the most common suffixes in a list or text file. I am having trouble iterating through the list or file and adding the suffixes to the dictionary with a counter for each time they appear.

file = open('suffix.txt', 'r')

def suffix(text):
    suffixList = {}
    count = 0
    read = file.readlines()
    Suffix = ""
    for line in read:
        line = line.strip('\n') #I am trying to strip the line character
        line = line[-3:] #Here I am trying to find 3-letter suffixes
        if line not in suffixList:
            suffixList[Suffix] = [line] #I want to add the suffix to the dic
            count += 1 # I want to add a counter for each suffix 


return suffixList
print (suffixList)


suffix(file)

You may use collections.Counter() with list comprehension as:

>>> from collections import Counter  
>>> my_name_list = ['Mr XYZ', 'Ms ABC', 'Dr 123', 'Er Hello Sir', 'Mr PQR', 'Dr XYZ']
>>> Counter(name[-3:] for name in my_name_list)
Counter({'XYZ': 2, '123': 1, 'Sir': 1, 'ABC': 1, 'PQR': 1})
#         ^ XYZ twice in list            ^ ABC once in list      

First, return should go inside the function. Second, Suffix in your code never changes, and for that reason, You won't achieve your goal. Third, count increases for every line no matter what suffix is.

file = open('suffix.txt', 'r')

def find_suffix(text):
    suffixList = {}
    for line in file.readlines():
        line = line.strip('\n')
        suffix = line[-3:]
        if suffix not in suffixList:
            suffixList[suffix] = 1
        else:
            suffixList[suffix] += 1

    return suffixList

find_suffix(file)

First time suffix occurs, add it to the list and set count to 1. Every time suffix reappears, just increase counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM