使用Python列出最常见的后缀

时间:2016-10-27 18:28:07

标签: python for-loop dictionary

我正在尝试编写一个能够在列表或文本文件中找到最常见后缀的函数。我无法遍历列表或文件,并且每次出现时都会使用计数器将后缀添加到字典中。

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)

2 个答案:

答案 0 :(得分:1)

您可以将collections.Counter()列表理解一起使用为:

>>> 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      

答案 1 :(得分:0)

首先,return应该进入函数内部。其次,代码中的Suffix永远不会改变,因此,您无法实现目标。第三,无论后缀是什么,每行都会count增加。

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)

首次出现后缀,将其添加到列表中并将计数设置为1.每次重复出现后缀时,只需增加计数器。