值错误:<解包的值太多> [英] ValueError: <Too many values to unpack>

查看:43
本文介绍了值错误:<解包的值太多>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务是使用 MongoDB 查找、排序和删除 'type': "homework" 并且分数最低的学生.我也尝试使用 toArray() 函数,但它给出了错误.现在我尝试在文档中继续作为计数器的数量,并删除得分最低的最后一个排序文档.

Task is to find,sort,and remove the student with 'type': "homework" and with the lowest score using MongoDB. I also tried to use toArray() function,but it gave an error. Now I try to move on in document as a number of counter and to remove the last sorted document with the lowest score.

import pymongo 
import sys       

#establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")

def delete_lowest_doc():

    #get a handle to the students database
    db=connection.students
    grades = db.grades


    try:
        for i in range(0,grades.find().count()):
            docs_1 = grades.find({'type':"homework", 'student_id':i}).sort(['score',-1])

            counter_1 = grades.find({'type':"homework",'student_id':i})
            counter_2 = counter_1.sort(['score',-1]).count() 

            while (counter_2>0):
                doc = docs_1.next();
                counter_2=counter_2-1;
            grades.remove(doc)


    except Exception as e:
        print ("Unexpected error:", type(e), e)

delete_lowest_doc()

推荐答案

collection.find 只需要一个位置参数,而你给了它两个.

collection.find only takes one positional argument and you're giving it two.

更改您的电话,使其看起来如下所示:grades.find({"type": "homework", "student_id": i}).

Change your calls so that they look like the following: grades.find({"type": "homework", "student_id": i}).

这篇关于值错误:<解包的值太多>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆