Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:01:35+00:00 2026-06-14T02:01:35+00:00

I have the following in my database in an object called item and I

  • 0

I have the following in my database in an object called “item” and I want to write some python that will select the lowest score from each group of student_id….I am a total python noob…how would I do this?

{u'student_id': 197, u'_id': ObjectId('50906d7fa3c412bb040eb88d'), u'type': u'homework', u'score': 10.90872422518918}
{u'student_id': 197, u'_id': ObjectId('50906d7fa3c412bb040eb88e'), u'type': u'homework', u'score': 88.3871242475841}
{u'student_id': 198, u'_id': ObjectId('50906d7fa3c412bb040eb892'), u'type': u'homework', u'score': 17.46279901047208}
{u'student_id': 198, u'_id': ObjectId('50906d7fa3c412bb040eb891'), u'type': u'homework', u'score': 76.18366499496366}
{u'student_id': 199, u'_id': ObjectId('50906d7fa3c412bb040eb895'), u'type': u'homework', u'score': 49.34223066136407}
{u'student_id': 199, u'_id': ObjectId('50906d7fa3c412bb040eb896'), u'type': u'homework', u'score': 58.09608083191365}

UPDATE:

having some trouble running my code…can I get some sanity checking done? I’m getting this error…

NameError: name 'getResultFromDatabase' is not defined

Thank you for your trouble….here is my code…

import pymongo

from itertools import groupby
from pymongo import Connection

data = getResultFromDatabase()
connection = Connection('localhost', 27017)

db = connection.students

item = db.grades.find({'type' : 'homework'}).sort([('student_id',pymongo.ASCENDING),('score',pymongo.ASCENDING)])



for id, items in groupby(data, lambda s: s['student_id']):
    lowest_score = min(i['score'] for i in items)

    print lowest_score
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:01:37+00:00Added an answer on June 14, 2026 at 2:01 am

    groupby does what you want:

    from itertools import groupby
    
    data = getResultFromDatabase()
    
    for id, items in groupby(data, lambda s: s['student_id']):
        lowest_score_entry = min(items, key=lambda i: i['score'])
        print lowest_score_entry['score'], lowest_score_entry['_id']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python noob here. I have the following in my database in an object called
I have some problems with deleting object. I am getting the following error: [Test.Item#ab9a9869-b2c1-4262-8d33-9dd9010abd96][SQL:
I have the following database table object: public class Goal { @DatabaseField(generatedId = true)
I have a html page that calls a php object to get some data
I have the following code that downloads an object from a 3rd party and
I have a custom object that maps a boolean value from a legacy database
I have a object that wraps all access to SQLAlchemy, let's called it Wrapper.
Lets say I have the following entities that map to database tables (every matching
I have the following bit of code to set a parameter that will be
I have the following database structure. ID | Name | Marks I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.