我有这种类型的 JSON 文档:
{
name : 'John',
age : 30,
counts : [
{day : monday, pay : 75, extra : 33} ,
{day : tuesday, pay : 69, extra : 12} ,
{day : sunday, pay : 42, extra : 0} ]
}
我想遍历它们并执行操作。我创建了一个变量“a”作为迭代的尝试:
x = [2, 67, 53, 21, 5, ... ... ]
for i in db.docs.find():
a = 0
while a <= len(i['counts']):
...
x.append(i['counts'][a]['extra'])
...
total = ...
...
a = a + 1
db.docs.update({'id': i['id']}, {'$set':{"counts.a.total":total}})
如何让 Python 接受“a”作为列表索引?感谢您的帮助。
这是错误消息:
Traceback (most recent call last):
x.append(i['counts'][a]['extra'])
IndexError: list index out of range