'I need to find n best students overall and m best students of each country will group together to form their national group

I managed to get the 32 best points. Now I am trying to get the index of 32 best students so that I can show who they are. The link to my json file is here:

https://drive.google.com/file/d/1OOkX1hAD6Ot-I3h_DUM2gRqdSl5Hy2Pl/view

And the code is below:

import json


file_path = "C:/Users/User/Desktop/leksion 10/testim/u2/olympiad.json"

with open(file_path, 'r') as j:

    contents = json.loads(j.read())
    print(contents)

print("\n================================================")

class Competitor:
    def __init__(self, first_name, last_name, country, point):
        self.first_name = first_name
        self.last_name = last_name
        self.country= country
        self.point = int(point)
    def __repr__(self):
        return f'{self.first_name} {self.last_name} {self.country} {self.point}'

olimpiade=[]

for i in contents:
    olimpiade.append(Competitor(i.get('first_name'),
                                i.get('last_name'),
                                i.get('country'),
                                i.get('point'),))
print(olimpiade)
print("\n================================================")


#32 nxënësit më të mirë do të kalojnë në fazën e dytë. Të ndërtohet një funksion i cili kthen konkurentët e fazës së dytë.
print("\n================================================")

print(type(olimpiade))
print(type(contents))
print(type(Competitor))

for i in contents:
    

print(a)

print("\n================================================")

for i in olimpiade:
    for j in i:
        L=olimpiade.sort(key=lambda x: x.point)
print(L)

I have tried this for example

pike=[]
for value in contents:
    pike.append(value['point'])
print(pike)


n = 32
  
pike.sort()
print(pike[-n:])


Solution 1:[1]

Using the data from your link and downloading to file 'olympiad.json'

Code

import json

def best_students(lst, n=1):
    '''
        Top n students
    '''
    return sorted(lst, 
                  key = lambda d: d['point'],  # sort based upon points
                  reverse = True)[:n]          # Take n talk students

def best_students_by_country(lst, m=1):
    '''
        Top m students in each country
    '''
    # Sort by country
    by_country = sorted(lst, key = lambda d: d['country'])
    
    groups = []
           
    for d in by_country:
        if not groups:
            groups.append([])
        elif groups[-1][-1]['country'] != d['country']:
              groups.append([])    # add new country
        # Append student
        groups[-1].append(d)  # append student to new country
        
    # List comprehension for best m students in each group
    return [best_students(g, m) for g in groups]
          

Usage

# Deserialize json file
with open('olympiad.json', 'r') as f:
    data = json.load(f)

# Top two students overall
print(best_students(data, 2))

# Top two students by country
print(best_students_by_country(data, 2))

Outputs

[{'first_name': 'Harvey',
  'last_name': 'Massey',
  'country': 'Bolivia',
  'point': 9999},
 {'first_name': 'Barbra',
  'last_name': 'Knight',
  'country': 'Equatorial Guinea',
  'point': 9998}]

[[{'first_name': 'Wade',
   'last_name': 'Dyer',
   'country': 'Afghanistan',
   'point': 9822},
  {'first_name': 'Terrell',
   'last_name': 'Martin',
   'country': 'Afghanistan',
   'point': 8875}],
 [{'first_name': 'Delaney',
   'last_name': 'Buck',
   'country': 'Albania',
   'point': 9729},
  {'first_name': 'Melton',
   'last_name': 'Ford',
   'country': 'Albania',
   'point': 9359}],
    ...

Solution 2:[2]

I have written how to make a useful dictionary out of your question.

Firstly, I am assuming all your values are in a list, and each value is a string

That would be texts

We can get list of countries from external sources

pip install country-list
from country_list import countries_for_language
countries = dict(countries_for_language('en'))
countries = list(countries.values())

Initialise empty dictionary - scores_dict = {}

for i in texts:
  for j in countries:
    if j in i:
      country = j
 
  score = [int(s) for s in i.split() if s.isdigit()]

  try:
    scores_dict[country].extend(score)
  except:
    scores_dict[country] = score

This will give you a dictionary that looks like this

{'Albania': [5287],
 'Bolivia': [1666],
 'Croatia': [1201],
 'Cyprus': [8508]}

From here, you can just iterate through each country to get top 5 students overall and top 5 students for each country.

Solution 3:[3]

From your file I created a dataframe in pandas. The general sorting is 'sorted_all'. 'ascending=False' means that the highest data will come first. In the national team, Mexico selected the best 7 players.

head() by default, it shows five values.

import pandas as pd

df = pd.read_json('olympiad.json')

sorted_all = df.sort_values(by='point', ascending=False)
sorted_national = df.sort_values(['country','point'], ascending=[True, False])

print(sorted_all.head())
print(sorted_national.loc[sorted_national['country'] == 'Mexico'].head(7))

Output all

     first_name last_name            country  point
1453     Harvey    Massey            Bolivia   9999
3666     Barbra    Knight  Equatorial Guinea   9998
5228    Rebecca   Navarro            Tunisia   9994
338      Jolene     Pratt             Mexico   9993
5322    Barnett   Herrera            Comoros   9986

Output national Mexico

     first_name last_name country  point
338      Jolene     Pratt  Mexico   9993
5118      Doyle   Goodman  Mexico   9980
2967      Mindy    Watson  Mexico   9510
6074      Riley      Hall  Mexico   9426
5357       Leah   Collins  Mexico   8798
5596        Luz  Bartlett  Mexico   8592
3684    Annette     Perry  Mexico   8457

Solution 4:[4]

There should be a grade range and grade of each student, that is what will help you filter the best students.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Vishal Balaji
Solution 3
Solution 4 Prince Jam