2012-11-21 211 views
0

我無法調試這個程序,它計算了許多鳴叫的P值:蟒蛇,while循環,P值

db = connection.data 
shows = db.individual 

def pvalue(): 
    #p-values of total tweets 
    show_records = sorted([(m['total_tweets'], m) for m in db.individual.find()]) 

    index=0 
    while index < len(show_records):  
     show = show_records[index] 
     tweet_pvalue = 1 - (index + 1.0)/len(show_records) 
     total=show['total_tweets'] 
     shows.update({"id": show[1]["id"]}, {'$set':{"pvalue_total_tweets":tweet_pvalue}}) 

    #need to remove several occurences of the same number of tweets to not false the p-value. 

     while show_records[index + 1]['total_tweets'] == total: #while next document has the same number of tweets 
      index+=1 
      show=show_records[index] 
      shows.update({"id": show[1]["id"]}, {'$set':{"pvalue_total_tweets":tweet_pvalue}}) 

它返回:

total=show['total_tweets'] 
TypeError: tuple indices must be integers, not str 

謝謝你這麼多你的幫助!

+1

你似乎認爲'show'是一個字典,但它不是,它是一個元組。 – Junuxx

+0

你想說show [0] – Himanshu

回答

1
show_records = sorted([(m['total_tweets'], m) for m in db.individual.find()]) 

這將返回一個元組列表,如[(1, {'total_tweets': 1}), (2, {'total_tweets': 2}]

而這一次

show = show_records[index] 

返回(例如,如果索引== 1) - (2, {'total_tweets': 2}),其是元組。而你正在嘗試做

(2, {'total_tweets': 2})['total_tweets'] 

它會導致錯誤。你應該寫:

(2, {'total_tweets': 2})[1]['total_tweets']