Python Forum
how to calculate frequency of word in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to calculate frequency of word in list
#1
Dear, 

 how to  calculate the frequency of the word in the list  if the word is not in the list  just return  word equals to  zero

Huh Huh Huh Huh Huh
Reply
#2
You can't, if something is not in the list.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
You can use count method of list, here is the documentation page:
https://docs.python.org/3.5/tutorial/dat...tures.html

About returning words equal to zero - a simple if statement that checks result of count method should do.
Give it a go and post the code if you will require further help.
Reply
#4
(Feb-28-2017, 01:41 PM)wavic Wrote: You can't, if something is not in the list.
Really?

from collections import Counter
words = ['alpha', 'bravo', 'charlie', 'bravo', 'charlie', 'bravo']
c = Counter(words)
print(c['bravo'])
print(c['echo'])
# another way
print(words.count('alpha'))
print(words.count('echo'))
Output:
3 0 1 0
Reply
#5
He is getting sero. This post is related to https://python-forum.io/Thread-how-can-r...below-code 

Why do you slice the list like this: mystr[:1]. Why don't you search for the whole string "domain ontologies"?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,446 Jan-05-2024, 08:30 PM
Last Post: sgrey
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,499 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Trying to get the first letter of every word in a list DanielCook 2 2,102 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  Trying to find first 2 letter word in a list of words Oldman45 7 3,634 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,525 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Python Speech recognition, word by word AceScottie 6 15,865 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Bulk Generating Cloze Deletions based on Tatoeba sentences and word frequency lists wizzie 10 5,060 Dec-23-2019, 12:16 PM
Last Post: wizzie
  print a word after specific word search evilcode1 8 4,725 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020