使用2个参数对列表中的单词进行计数的功能

时间:2018-08-26 05:05:33

标签: python

嗨,我正在尝试创建一个函数来对文本列表中出现的单词进行计数,以将结果生成为['a','1'],['b','4'],['c' ,'5'] ...

这是我尝试执行的操作,但是它不起作用,并且我还没有学习计数功能。

file_name = input('what file would you like to open? : ')
objecthere = open(file_name,'r')
argument = objecthere.read()
word_list = argument.split()


def word_frequency(words_from_list, word_frequency):
    new_list = []
    for word in word_list:
        if word in word_list:
            new_list.index(word)[1] += 1
        else:
            new_list.append([word,0])
    print(new_list)

我是在正确的轨道上吗,还是有其他方法?

编辑:我试图弄清楚如何使用word_frequency(word_list,3)会给我出现前3个单词的方式,例如['c','3'],['b' ,'2'],['a','1']。任何帮助或意见将不胜感激!

4 个答案:

答案 0 :(得分:0)

我在移动设备上,因此无法检查,但我相信您在检查结果列表中的出现时名称错误:

def word_frequency(words_from_list, word_frequency):
    new_list = [] 
    for word in word_list:
        if word in word_list:
            new_list.index(word)[1] += 1
        else:
            new_list.append([word,0])
    print(new_list)

更正:

def word_frequency(words_from_list, word_frequency):
    new_list = [] 
    for word in word_list:
        if word in new_list:
            new_list.index(word)[1] += 1
        else:
            new_list.append([word,0])
    print(new_list)

答案 1 :(得分:0)

根据gilch的建议,我们可以使用Collection.Counter并拆分以创建列表

from collections import Counter
lst = 'what file would you like to open? : '.split()
Counter(lst)

答案 2 :(得分:0)

>>> from collections import Counter
>>> word_list = ['a','b','b','c','c','c','d']
>>> Counter(word_list).most_common()
[('c', 3), ('b', 2), ('a', 1), ('d', 1)]

这使用元组将它们配对,这可能对您有用,但是,如果您希望将它们作为字符串列表,则

>>> [[k, str(v)] for k, v in _]
[['c', '3'], ['b', '2'], ['a', '1'], ['d', '1']]

如果您更喜欢按字母顺序而不是按频率顺序排列,

>>> [*sorted(_)]
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '1']]

答案 3 :(得分:0)

也许尝试字典。

def word_frequency(words_from_list, word_frequency):
    dic = {}
    for word in word_list:
        if word in dic:
            dic[word] += 1
        else:
            dic[word] = 1
    # You can convert tuple pairs to list and sort it like @glitch did.
    print(list(dic.items()))