2014-03-03 251 views
0

我得到一個IndexError:列表索引超出範圍錯誤。我已經收到每封郵件的收件人列表。我已將收件人列表摺疊到一個列表中。我如何解決這個問題?python IndexError:列表索引超出範圍

import json 
import pymongo # pip install pymongo 
from bson import json_util # Comes with pymongo 
import re 
from pymongo import MongoClient 
# The basis of our query 
FROM = "[email protected]" 


client = pymongo.MongoClient('mongodb://user:[email protected]:33499/enron') 
db = client.enron 
mbox = db.mbox 

# Get the recipient lists for each message 

recipients_per_message = db.mbox.aggregate([ 
    {"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}}, 
    {"$project" : {"From" : 1, "To" : 1} }, 
    {"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } }      
    ])['result'][0]['recipients'] 

# Collapse the lists of recipients into a single list 

all_recipients = [recipient 
        for message in recipients_per_message 
        for recipient in message] 

# Calculate the number of recipients per sent message and sort 

recipients_per_message_totals = \ 
    sorted([len(recipients) 
    for recipients in recipients_per_message]) 

# Demonstrate how to use $unwind followed by $group to collapse 
# the recipient lists into a single list (with no duplicates 
# per the $addToSet operator) 

unique_recipients = db.mbox.aggregate([ 
    {"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}}, 
    {"$project" : {"From" : 1, "To" : 1} }, 
    {"$unwind" : "$To"}, 
    {"$group" : {"_id" : "From", "recipients" : {"$addToSet" : "$To"}} } 
    ]['result'][0]['recipients']) 

print all_recipients 
print "Num total recipients on all messages:", len(all_recipients) 
print "Num recipients for each message:", recipients_per_message_totals 
print "Num unique recipients", len(unique_recipients) 

這是回溯

IndexError Traceback (most recent call last) 
    <ipython-input-85-b1e01d6382fb> in <module>() 
    18 {"$project" : {"From" : 1, "To" : 1} }, 
    19 {"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } } 
    --->20 ])['result'][0]['recipients'] 
    21 
    22 # Collapse the lists of recipients into a single list 

    IndexError: list index out of range 
+0

請包括您的錯誤的完整追溯;它告訴我們發生了什麼異常,什麼叫該行,等等。 –

+0

''result''可能是一個空列表。 'db.mbox.aggregate(...)'''''result'] [0] ['recipients']'除外的結果是什麼? – Matt

+0

沒有。錯誤的診斷。你的聚合沒有返回任何結果。這是錯誤來自的地方。所以請在你的問題中顯示你的文件的樣本。這意味着一些將要匹配的文檔。 –

回答

0

其實,改變這個:

{"$match" : {"From" : {"$regex": "^" + FROM, "$options": "i"} }}, 

這是你的卡?

如果是這樣,那麼它看起來像你試圖插入一個實際的正則表達式到MongoDB真正想要的字符串中。因此,形式。

P.S Drop不區分大小寫的匹配。這是沒有用的,因爲你的整個系列正在被掃描。相反,請將所有電子郵件地址保存爲小寫字母。無論如何,個案對電子郵件無效。小寫您的存儲空間和您的輸入。一切都會更快。

相關問題