Search code examples
pythonbinning

Grouping data together based on specific increment


I have a 2D list in python like:

[['Xzavier Kaska', 1.04], ['Brent Barnaby', 1.13], ['Alena Holoien', 1.37], 
 ['Sam Surey', 1.37], ['Kash Nocella', 1.55], ['Ezequiel Gerraughty', 1.57], 
 ['Myah Linsley', 1.74], ['Jaelynn Dzur', 1.79], ['Alfredo Andrew', 1.83], 
 ['Skylar Movius', 1.95], ['Raphael Nocella', 2.14], ['Alondra Wallace', 2.2],
 ['Clark Loomis', 2.3], ['Skylar Cvek', 2.36], ['Carson Racugno', 2.52], 
 ['Kathy Viveros-aguilera', 2.62], ['Heaven Barnaby', 2.75], 
 ['Rebekah\tSpartichino', 3.24], ['Semaj Abernathy', 3.35], ['Rylee Dalton', 3.38], 
 ['Sterling Grove', 3.46], ['Rebekah Ghosh', 3.85]]

where index 0 represents student name and index 1 represents gpa of each student.

I want to group two pairs together in the above data based on increments of gpa's. Example: group them by increments of 1.0... students who have gpa 0.0-1.0 are unioned together, e.g. [0.0-0.1), [0.1-0.2), ...[3.9-4.0]; [0.0-0.5],[0.5-1.0),...[3.5-4] ; [0-1),[1,2),[2,3),[3,4).


Solution

  • You can create a dictionary and set key as a range, like if you want range 0-1 key of dict will be 1, range 4-5 key of dict will be 5 etc. So we can create a function that will group students with certain pace:

    import math
    
    l = [['Xzavier Kaska', 1.04], ['Brent Barnaby', 1.13], ['Alena Holoien', 1.37],
    ['Sam Surey', 1.37], ['Kash Nocella', 1.55], ['Ezequiel Gerraughty', 1.57], 
    ['Myah Linsley', 1.74], ['Jaelynn Dzur', 1.79], ['Alfredo Andrew', 1.83], 
    ['Skylar Movius', 1.95], ['Raphael Nocella', 2.14], ['Alondra Wallace', 2.2],
    ['Clark Loomis', 2.3], ['Skylar Cvek', 2.36], ['Carson Racugno', 2.52], 
    ['Kathy Viveros-aguilera', 2.62], ['Heaven Barnaby', 2.75], 
    ['Rebekah\tSpartichino', 3.24], ['Semaj Abernathy', 3.35], ['Rylee Dalton', 3.38], 
    ['Sterling Grove', 3.46], ['Rebekah Ghosh', 3.85]]
    
    def group(l : list):
       """
       Group list students by their gpa with pace 1
       :param l: input list of students and gpa's
       :return: dictionary, where key is range: (key-1, key)
       ex. if you want to get list of students with gpa 1 :  d[1]
       """
    
       d = {}
       i = 0
       for student, gpa in l:
           index = math.ceil(gpa) - 1
           if index < 1: index = 1
           if index not in d.keys():
               d[index] = [[student, gpa]]
           else:
               d[index].append([student, gpa])
           i += 1
    
       return d
    
    d = group(l)
    
    # checking:
    
    for i in group(l):
       print(f'range : {i-1} to {i}, list : {d[i]}')
    
    # output will be:
    # range : 0 to 1, list : [['Xzavier Kaska', 1.04], ['Brent Barnaby', 1.13], ['Alena Holoien', 1.37], ['Sam Surey', 1.37], ['Kash Nocella', 1.55], ['Ezequiel Gerraughty', 1.57], ['Myah Linsley', 1.74], ['Jaelynn Dzur', 1.79], ['Alfredo Andrew', 1.83], ['Skylar Movius', 1.95]]
    # range : 1 to 2, list : [['Raphael Nocella', 2.14], ['Alondra Wallace', 2.2], ['Clark Loomis', 2.3], ['Skylar Cvek', 2.36], ['Carson Racugno', 2.52], ['Kathy Viveros-aguilera', 2.62], ['Heaven Barnaby', 2.75]]
    # range : 2 to 3, list : [['Rebekah\tSpartichino', 3.24], ['Semaj Abernathy', 3.35], ['Rylee Dalton', 3.38], ['Sterling Grove', 3.46], ['Rebekah Ghosh', 3.85]]
    

    Using dicts you can check numbers of students in certain group if group is not empty:

    if 4 in d.keys():
        print(len(d[4]))
    else:
        print('No students in such a group')