Home > Net >  How do I check how many times a term has occured in a list and its frequency?
How do I check how many times a term has occured in a list and its frequency?

Time:12-09

text=["duran duran sang wild boys in 1984","wild boys don't remain forever wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]

For example in this list "duran" is in 1 sentence and has occured 2 times in text[0]

For seperate codes first output should be 1 and second output should be 2

I tried functions with .count and .check but couldn't make a proper code.

CodePudding user response:

This code counts all occurencces of a word in all strings in a list of lists l and also returns the number of list elements containing this word.

text=["duran duran sang wild boys in 1984","wild boys don't remain forever  wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]

def countInListOfLists(l, word):
    counts = [s.count(word) for s in l]
    return sum([1 for c in counts if c > 0]), sum(counts)

print(countInListOfLists(text, "duran"))

Output:

(1,2)

CodePudding user response:

You can do this as a one liner -

wordCount = len([word for word in (' ').join(text).split(' ') if word == 'duran'])

CodePudding user response:

you could accomplish it with the following code.

# check how many times a term has occurred in a list and its frequency
def count_occurrences(lst, term):
    return lst.count(term)

# check the frequency of term in a list
def frequency(lst, term):
    return count_occurrences(lst, term) / len(lst)

Additionally, I am assuming you would split your list elements in advance to create a list of words rather than a list of sentences, which adds complexity. Otherwise, see the @CLRW97 response.

Split your list into words:

# split list of sentences into list of words
def split_sentences(lst):
    return ' '.join(lst).split()

As a result, you will have three clean functions at your disposal:

# split list of sentences into list of words
def split_sentences(lst):
    return ' '.join(lst).split()

# check how many times a term has occurred in a list and its frequency
def count_occurrences(lst, term):
    lst = split_sentences(lst)
    return lst.count(term)

# check the frequency of term in a list
def frequency(lst, term):
    lst = split_sentences(lst)
    return count_occurrences(lst, term) / len(lst)

text=["duran duran sang wild boys in 1984","wild boys don't 

remain forever  wild","who brought wild flowers","it was john krakauer who wrote in to the wild"]

print(count_occurrences(text, "duran"))
print(frequency(text, "duran"))

> 2
> 0.07407407407407407

However, for a more straightforward answer, check out @DannyMoshe is response, which appears to be rather intelligent as well. Mine is simply deconstructed into a few functions to make the code more understandable while also reducing its complexity by utilising builtins/simple Python functions.

  • Related