最常用的是字符串中的单词

时间:2013-11-24 03:09:03

标签: python python-3.x

如何在不导入任何内容的情况下找到字符串中最常用的单词?

>>> x = "In the sky, I fly fly away"
>>> common(x) 
'fly'

到目前为止,这是我的代码,但我遇到了问题。

def common(x):
    dict = {}
    for i in x:
        try:
            dict[i] += 1
        except KeyError:
            dict[i] = 1
    return (sorted(dict.items(), key = lambda k: k[1], reverse = True)[:1])

2 个答案:

答案 0 :(得分:2)

改为返回第一个键(按反向排序顺序):

return sorted(dict, key=dict.get, reverse=True)[0]

尽量避免命名变量dict;这掩盖了内置类型,只是为了混淆。例如,使用count代替。这是一个更简单的版本:

def common(f):
    count = {}
    max = None
    for i in f:
        count[i] = count.get(i, 0) + 1
        if count[i] > count.get(max, 0):
            max = i
    return max

请注意,此版本完全取消了排序。相反,我们会在计算时更新max

您还需要记住先将句子分成单词:

>>> common(x.split())
'fly'

答案 1 :(得分:0)

基本上,您只需创建一个字数字典,反向排序并渲染列表中的第一个元素。 下面的代码是Python 3。

注意: 您需要调整单词解析逻辑以适合您的喜好(例如忽略标点符号等)。

不使用导入的代码解决方案

def get_word_counts(words):
    word_list = words.split()
    word_counts = {}
    for word in word_list:
        if word in word_counts.keys():
            word_counts[word] +=1
        else:
            word_counts[word] = 1

    return word_counts.items()

print(sorted(get_word_counts( "In the sky, I fly fly away"), 
      key=lambda x: x[1], reverse=True))
print("Most frequent word in phrase is: ", 
       list(sorted(get_word_counts( "In the sky, I fly fly away"), 
                   key=lambda x: x[1], reverse=True))[0][0])

我不确定为什么你不想使用导入它,因为标准库有一个更简洁的方法来获得相同的最终结果。

使用导入的代码(更短)

print (sorted(Counter("In the sky, I fly fly away".split()).items(),
       key=operator.itemgetter(1), reverse=True))
print("Most frequent word in phrase is: ",
       sorted(Counter("In the sky, I fly fly away".split()).items(),
       key=operator.itemgetter(1), reverse=True)[0][0])