-2

Hi i am trying to create a function to count the word occurence in a list of text to produce the result as ['a','1'], ['b' ,'4'] , ['c' , '5']......

this is what i tried to do but its not working and i have not learn the count function yet.

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)

Am i on the right track or is there another way?

Edited : Im trying to figure out how do i use the function call in the way that word_frequency(word_list,3) will give me the top 3 word occurence like ['c','3'],['b' , '2'] , ['a' , '1']. Any help or input will be appreciated!

cwerwf
  • 13
  • 5
  • Try [`collections.Counter`](https://docs.python.org/3/library/collections.html?highlight=collections#collections.Counter). – gilch Aug 26 '18 at 05:08
  • Obviously, `word` *is* `in word_list`, since that's where you got it from in the first place. So that if-statement isn't doing anything useful. – gilch Aug 26 '18 at 05:09
  • Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – cwerwf Aug 28 '18 at 06:29

4 Answers4

0

I am on mobile so I can't check but I believe you've names the wrong list when checking for the occurrence in your result list:

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)

Correction:

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)
Sonic7662
  • 66
  • 6
  • 1
    If you're on Andriod, you can run Python as an app like [QPython3](https://play.google.com/store/apps/details?id=org.qpython.qpy3), or in a shell app like [Termux](https://play.google.com/store/apps/details?id=com.termux). They're free. – gilch Aug 26 '18 at 05:26
0

As suggested by gilch we can use Collection.Counter and split to make list

from collections import Counter
lst = 'what file would you like to open? : '.split()
Counter(lst)
PIG
  • 599
  • 3
  • 13
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)]

This pairs them using tuples, which is probably fine for your purposes, but if you want them as lists of strings,

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

And if you prefer them alphabetical instead of in frequency order,

>>> [*sorted(_)]
[['a', '1'], ['b', '2'], ['c', '3'], ['d', '1']]
gilch
  • 10,813
  • 1
  • 23
  • 28
0

Maybe try dictionary.

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()))
Frank
  • 1,215
  • 12
  • 24