我得到一个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 = "kenneth.lay@enron.com"
client = pymongo.MongoClient('mongodb://user:user123@ds033499.mongolab.com: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)
实际上,改变这个:
{"$match" : {"From" : {"$regex": "^" + FROM, "$options": "i"} }},
这是你的卡吗?
如果是这样,那么看起来你试图将一个真正的正则表达式插入到MongoDB真正想要的字符串中。因此形式。
P.S 丢弃不区分大小写的匹配项。这是没用的,因为你的整个系列正在被扫描。而是将所有电子邮件地址保留为集合中的小写字母。案件无论如何都不适用于电子邮件。小写您的存储和输入。一切都会更快。