from csv import reader from operator import attrgetter class Subject: def __init__(self, subjectString, criteria): splitString = subjectString.split(" ") self.faculty = splitString[0] self.code = splitString[1] self.name = " ".join(splitString[2:]) self.criteria = criteria def subjectsWithoutLimit(subjectList): return len([subject for subject in subjectList if subject.criteria == 'Alle']) def averageLimit(subjectList): subjectsWithLimits = [subject for subject in subjectList if subject.criteria != 'Alle'] return sum([float(subject.criteria) for subject in subjectsWithLimits]) / len(subjectsWithLimits) def minLimit(subjectList): subjectsWithLimits = [subject for subject in subjectList if subject.criteria != 'Alle'] for subject in subjectsWithLimits: subject.criteria = float(subject.criteria) return min(subjectsWithLimits, key=attrgetter('criteria')) def getFacultySubjects(subjectList): toDict = lambda subject: {subject.name: subject.criteria} result = {} for subject in subjectList: if not subject.faculty in result: result[subject.faculty] = [] result[subject.faculty].append(toDict(subject)) return result if __name__ == "__main__": with open('poenggrenser_2011.csv' ,'r') as file: Subjects = [Subject(*i) for i in reader(file)] print('Antall studier hvor alle kom inn:', subjectsWithoutLimit(Subjects)) print('Gjennomsnittlig opptaksgrense for NTNU var:', averageLimit(Subjects)) formatSubject = lambda subject: f'{subject.faculty} {subject.code} {subject.name}' print('Studiet som hadde den laveste opptaksgrensen var:', formatSubject(minLimit(Subjects))) print(getFacultySubjects(Subjects))