2015-01-02 31 views
1

我正在閱讀關於編程集體智能中搜索引擎的章節,並且遇到以下代碼片段並試圖在IPython中實現它。然而,我遇到了錯誤:如何解決以下錯誤?

def getmatchrows(self,q): 
    fieldlist='w0.urlid' 
    tablelist='' 
    clauselist='' 
    wordids=[] 

    words=q.split() 
    tablenumber=0 

    for word in words: 
     wordrow=self.con.execute("select rowid from wordlist where word='%s'" % word).fetchone() 
     if wordrow!=None: 
      wordid=wordrow[0] 
      wordids.append(wordid) 
      if tablenumber>0: 
       tablelist+=',' 
       clauselist+=' and ' 
       clauselist+='w%d.urlid=w%d.urlid and '%(tablenumber-1,tablenumber) 
      fieldlist+=',w%d.location'%tablenumber 
      tablelist+='wordlocation w%d'%tablenumber 
      clauselist+='w%d.wordid=%d'%(tablenumber,wordid) 
      tablenumber+=1 
    fullquery="SELECT %s FROM %s WHERE %s"%(fieldlist,tablelist,clauselist) 
    cur=self.con.execute(fullquery) 
    rows=[row for row in cur] 
    return row,wordids 

我得到了以下錯誤:

TypeError         Traceback (most recent call last) 
<ipython-input-25-e5fade95f22f> in <module>() 
----> 1 e.getmatchrows('data analysis') 

C:\Users\Blue\Anaconda\searchengine.pyc in getmatchrows(self, q) 
    128           tablelist+=',' 
    129           clauselist+=' and ' 
--> 130           clauselist+='w%d.urlid=w%d.urlid and '%tablenumber- 1,tablenumber 
131         fieldlist+=',w%d.location'%tablenumber 
132         tablelist+='wordlocation w%d'%tablenumber 

TypeError: unsupported operand type(s) for &: 'str' and 'tuple' 
+1

在跟蹤中,'%後'沒有括號,這是正常的嗎? – fredtantini

+0

嘿,你是否低估了這個問題? –

回答

0

認沽括號在行130:

tablenumber-1,tablenumber>>(tablenumber1,tablenumber)