0

I am having trouble debugging this program which calculates the p values for a number of tweets:

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}})

It returns:

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

Thank you so much for your help!

4

1 に答える 1

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']
于 2012-11-21T10:37:46.853 に答える