2014-10-01 78 views
0

我對Python非常陌生,我正在嘗試編寫一個GUI程序來拉取最頻繁,最不頻繁的名稱事件從班級作業的列表中選擇。我一直對這個代碼獲得一個(Python新手)繼續收到「TypeError:'int'對象不可調用」

TypeError: 'int' object is not callable

錯誤,而且我知道它與線做:

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() 
+1

什麼是 「listMyData」? – michaelb 2014-10-01 01:51:29

+1

如果我們不知道任何方法和屬性是做什麼的,那麼很難理解正在發生的事情。請將[MCVE](http://stackoverflow.com/help/mcve)放在一起,以自包含的方式顯示您的問題。 – MattDMo 2014-10-01 01:52:38

+0

對不起。 listMyData是一個與GUI中的文本框相對應的事件。我正在使用wxFormBuilder創建一個接口,將數字插入列表框中,然後單擊兩個按鈕中的一個來查找最頻繁的名稱或最不頻繁的名稱。 – 2014-10-01 01:58:24

回答

5

CountmyListData屬性。你在它後面加上括號「()」,就好像它是一個函數,但它不是。這只是一個整數值。這將是這樣做的:

y = 5 
x = y(word) 

這是沒有意義的。我不確定你想要用wordmyListData.Count來做什麼,但是也許你要找的是self.myListData[word].Count

As user3666197 mentioned,您還需要將float[...]更改爲float(...)

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 
>>> 
相關問題