By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

problems with extracting high frequency words with class in python

P: 3
this question may seem stupid for those who are familier with Python. however, it puzzles me a lot. when i tried to extrect words whose frequency is above 200, following codes works, but how to implement this function with "class"is a problem.
Expand|Select|Wrap|Line Numbers
  1. v= [('make', 207), ('one', 206), ('people', 196), ('go', 189), ('say', 180), ('get', 160), ('time', 150), ('take', 141), ('would', 137), ('like', 134), ('b', 130), ('know', 119), ('find', 118), ('woman', 117), ('work', 117), ('c', 116), ('d', 114), ('use', 114), ('two', 113), ('year', 108), ('book', 106), ('think', 106), ('come', 104), ('good', 100), ('man', 100), ('child', 98), ('new', 98), ('way', 95), ('day', 93)]
  2. a = []
  3. for q,w in v: 
  4.   if w > 200:
  5.     a.append(q)
i have tried following codes,but i did not work.
Expand|Select|Wrap|Line Numbers
  1. class cs(object):
  2.     def __init__(self,text):
  3.         self.a = text
  4.         self.w = []
  5.     def caltext(self):
  6.         for q,v in self.a:
  7.             if v > 200:
  8.                 self.w.append(q)
  9.         return w
  10.  
v1 = cs(v)
v11 = v1.caltext()
May 10 '16 #1

✓ answered by dwblas

Expand|Select|Wrap|Line Numbers
  1.                 self.w.append(q)
  2.          return w 
self.w is not the same as w (which has not been declared). Use self.w instead. Note that you don't have to return it since it is an instance object (self) and can be seen and used throughout the class. So
Expand|Select|Wrap|Line Numbers
  1. v1 = cs(v)
  2. v1.caltext()
  3. print v1.w 
Finally, the Python Style Guide suggests CamelCase for class names and using the guide helps other read your code https://www.python.org/dev/peps/pep-0008/

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 616
Expand|Select|Wrap|Line Numbers
  1.                 self.w.append(q)
  2.          return w 
self.w is not the same as w (which has not been declared). Use self.w instead. Note that you don't have to return it since it is an instance object (self) and can be seen and used throughout the class. So
Expand|Select|Wrap|Line Numbers
  1. v1 = cs(v)
  2. v1.caltext()
  3. print v1.w 
Finally, the Python Style Guide suggests CamelCase for class names and using the guide helps other read your code https://www.python.org/dev/peps/pep-0008/
May 12 '16 #2

P: 3
thanks for your answer, it works
May 12 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.