I'm creating a bunch of lists that looks like this:
我正在创建一堆看起来像这样的列表:
[ObjectId('542de00c763f4a7f558be1cf')]
[ObjectId('545284e7bf2e4ea2778b479d'), ObjectId('542de00c763f4a7f558be1d1')]
...etc
All of them have at least one item. When I run this loop, however:
他们都至少有一个项目。但是,当我运行此循环时:
for agent_id in agent_ids:
curs_obj = user_coll_obj.find({"agent_id" : str(agent_id)}).distinct("_id")
temp_list=[]
for obj in curs_obj:
temp_list.append(obj)
# print len(temp_list)
print "len(temp_list) is" , len(temp_list)
print "temp_list is" , temp_list
print "temp_list[0] is", temp_list[0]
I get:
print "temp_list[0] is", temp_list[0]
IndexError: list index out of range
If I comment out print "temp_list[1] is", temp_list[0]
I get:
如果我注释掉打印“temp_list [1] is”,temp_list [0]我得到:
Len(temp_list) is 2
temp_list is [ObjectId('542de00c763f4a7f558be1cb'), ObjectId('54c123d2bf2e4e140e8b45fc')]
....etc.
Why am I getting the error?
为什么我收到错误?
1 个解决方案
#1
Since it's a loop, I'm assuming that at one point your query returns an empty list for a given agent_id which crashes the script at temp_list[0]
since it's empty.
因为它是一个循环,所以我假设你的查询在某一点返回一个给定agent_id的空列表,该列表崩溃了temp_list [0]处的脚本,因为它是空的。
off topic mongo's distinct
returns a list so there's no need to loop through it and append it to another list (unless you have a good reason to do so).
关闭主题mongo的distinct返回一个列表,因此不需要循环它并将其附加到另一个列表(除非你有充分的理由这样做)。
#1
Since it's a loop, I'm assuming that at one point your query returns an empty list for a given agent_id which crashes the script at temp_list[0]
since it's empty.
因为它是一个循环,所以我假设你的查询在某一点返回一个给定agent_id的空列表,该列表崩溃了temp_list [0]处的脚本,因为它是空的。
off topic mongo's distinct
returns a list so there's no need to loop through it and append it to another list (unless you have a good reason to do so).
关闭主题mongo的distinct返回一个列表,因此不需要循环它并将其附加到另一个列表(除非你有充分的理由这样做)。