problems with extracting high frequency words with class in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • centella
    New Member
    • May 2016
    • 3

    problems with extracting high frequency words with class in python

    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.
    Code:
    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)]
    a = []
    for q,w in v: 
      if w > 200:
        a.append(q)
    i have tried following codes,but i did not work.
    Code:
    class cs(object):
        def __init__(self,text):
            self.a = text
            self.w = []
        def caltext(self):
            for q,v in self.a:
                if v > 200:
                    self.w.append(q)
            return w
    v1 = cs(v)
    v11 = v1.caltext()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Code:
                    self.w.append(q)
             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
    Code:
    v1 = cs(v)
    v1.caltext()
    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/

    Comment

    • centella
      New Member
      • May 2016
      • 3

      #3
      thanks for your answer, it works

      Comment

      Working...