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
請包括您的錯誤的完整追溯;它告訴我們發生了什麼異常,什麼叫該行,等等。 –
''result''可能是一個空列表。 'db.mbox.aggregate(...)'''''result'] [0] ['recipients']'除外的結果是什麼? – Matt
沒有。錯誤的診斷。你的聚合沒有返回任何結果。這是錯誤來自的地方。所以請在你的問題中顯示你的文件的樣本。這意味着一些將要匹配的文檔。 –