python索引器:列表索引超出范围
我得到一个索引器:列表索引超出范围错误。我已经得到了每封邮件的收件人列表。我已将收件人列表折叠为一个列表。我如何解决这个问题python索引器:列表索引超出范围,python,mongodb,python-2.7,Python,Mongodb,Python 2.7,我得到一个索引器:列表索引超出范围错误。我已经得到了每封邮件的收件人列表。我已将收件人列表折叠为一个列表。我如何解决这个问题 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@enr
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
索引器错误回溯(最近一次调用)
在()
18{“$project”:{“From”:1,“To”:1},
19{“$group”:{“\u id”:“$From”,“recipients”:{“$addToSet”:“$To”}}
--->20])['result'][0]['recipients']
21
22#将收件人列表折叠为一个列表
索引器:列表索引超出范围
实际上,更改此选项:
{"$match" : {"From" : {"$regex": "^" + FROM, "$options": "i"} }},
这是你的名片吗
如果是这样的话,那么看起来您正试图将一个实际的正则表达式插入MongoDB真正想要的字符串中。这就是形式
p.S删除不区分大小写的匹配。这是没有用的,因为你的整个收藏都被扫描了。在你的收藏中,所有的电子邮件地址都要用小写字母。无论如何,案例对电子邮件无效。将存储和输入的大小写为小写。一切都会更快。请包括对错误的完整回溯;它告诉我们异常发生在哪一行,以及调用该行的内容,等等。
“结果”
可能是一个空列表。db.mbox.aggregate(…)
排除['result'][0]['recipients']
的结果是什么。错误的诊断。您的聚合未返回任何结果。这就是错误的来源。因此,请在你的问题中展示你的文件样本。这意味着一些文件将会匹配,甚至更好。将聚合管道声明为调用外部的变量,并使用pp
转储该变量。然后我们(和你)可以看到你的re在dict中是如何解析的。那么发生了什么?是不是因为您尝试实现正则表达式的方式不起作用?