(Python新手)继续得到一个" TypeError:' int'对象不可调用"

时间:2014-10-01 01:49:09

标签: python typeerror

我对Python非常陌生,我试图为GUI编写一个程序,从列表中为类分配提取最频繁和最不频繁的名称。我一直在

  

TypeError:' int'对象不可调用

此代码出错,我知道这与该行有关:

word_frequencies += float[self.listMyData.Count(word)]/len[self.listMyData]  

但我不确定错误在说什么。我在这里看了类似的问题,但仍然不确定我做错了什么。任何帮助将不胜感激。

以下是完整的代码:

import wx
import myLoopGUI

class MyLoopFrame(myLoopGUI.MyFrame1):
    def __init__(self, parent):
        myLoopGUI.MyFrame1.__init__(self, parent)

    def clkAddData(self,parent):
        if len(self.txtAddData.Value) != 0:
            try:
                myname = str(self.txtAddData.Value)
                self.listMyData.Append(str(myname))
            except:
                wx.MessageBox("This has to be a name!")            
        else:
            wx.MessageBox("This can't be empty")




    def clkFindMost(self, parent):
        unique_words = []
        for word in range(self.listMyData.GetCount()):
                if word not in unique_words:
                    unique_words += [word]
        word_frequencies = []
        for word in unique_words:
            word_frequencies += float[self.listMyData.Count(word)]/len[self.listMyData]  

        max_index = 0
        frequent_words =[]
        for i in range(len(unique_words)):
            if word_frequencies[i] >= word_frequencies[max_index]:
                max_index = i
                frequent_words += unique_words[max_index]
        self.txtResults.Value = frequent_words



myApp = wx.App(False)
myFrame = MyLoopFrame(None)
myFrame.Show()
myApp.MainLoop()

2 个答案:

答案 0 :(得分:5)

CountmyListData的属性。你把括号" ()"在它背后,就好像它是一个功能,但它不是。它只是一个整数值。这就像这样做:

y = 5
x = y(word)

哪个没有意义。我不确定您尝试使用wordmyListData.Count做什么,但也许您正在寻找的是self.myListData[word].Count

As user3666197 mentioned,您还想将float[...]更改为float(...)

答案 1 :(得分:1)

omni has explained .Count财产问题。

您可能还需要修改 word_frequencies += float[ ... 这一行:

>>> float[ 5 ]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object has no attribute '__getitem__'
>>> float( 5 )
5.0
>>>