2013-06-30 21 views
2

我有一個輸入文件,其中的每一行都有一組單詞。接下來,我將查詢並計算輸入文件的每一行與查詢之間的相似性分數。我試圖用字典來做到這一點,保持字典的最大尺寸爲20(以獲得前20名的結果,如果通過排序值) 但我發現了以下語法錯誤:字典語法錯誤,同時獲得使用python中的詞典前20名的結果

File "retrieve.py", line 47 
result.item()[20].key()=temp 
SyntaxError: can't assign to function call 

我的代碼是:

images=open("jcnout","r") 
res=open("result","w") 
while linecount>0:  //linecount is the num of lines in file images 
    line=images.readline().split() 
    ////compute score for each line 
    if (len(result)<20): 
     result[str(line)]=score 
    else: 
     if(len(result)==20): 
      result = sorted(result.iteritems(), key=operator.itemgetter(1)) 
      if(result.item()[20].value()<score): 
       result.item()[20].key()=str(line) 
       result.item()[20].value=score 
       result = sorted(result.iteritems(), key=operator.itemgetter(1)) 
    res.write(result) 
    linecount-=1 

回答

2

,你不能分配給一個函數調用:

result.item()[20].key()=str(line) 

這是你想要做什麼!

因爲我不明白你是如何得到result的對象,我不能給你一個更好的建議,如何改變result.item()[20]的價值。但是,如果它是一本字典,你最好這樣做:

result.item()[20][str(line)] = score 
+0

謝謝,這工作:) – nish

0

您在這裏調用一個函數,並試圖將返回的值,另一個值:由於錯誤說

result.item()[20].key()=str(line) 
+0

感謝您的答覆,我得到了我的錯誤 – nish