0
IndexError :리스트 인덱스가 범위를 벗어납니다. Error. 나는 각 메시지에 대한 수신자 목록을 얻었습니다. 수신자 목록을 단일 목록으로 접었습니다. 어떻게하면 그 문제를 해결할 수 있습니까?python IndexError :리스트 인덱스가 범위를 벗어났습니다.
{"$match" : {"From" : {"$regex": "^" + FROM, "$options": "i"} }},
이이 카드가 :
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''는 아마도 빈 목록 일 것입니다. '[ 'result'] [0] [ 'recipients']'를 제외한'db.mbox.aggregate (...)'의 결과는 무엇입니까? – Matt
Nope. 진단 정보가 잘못되었습니다. 귀하의 집계 결과가 반환되지 않습니다. 그것이 오류의 출처입니다. 따라서 귀하의 질문에 귀하의 문서 견본을 보여주십시오. 이것은 일치시킬 문서 중 일부를 의미합니다. –