6

My program can output every student's name and their 3 scores from a dict in a file, but I need to sort the data by alphabetical order. How can I sort the names and scores alphabetically according to the surname?

This is my code so far: import pickle

def clssa():
    filename="friendlist.data"
    f=open('Class6A.txt','rb')
    storedlist = pickle.load(f)
    for key, value in storedlist.items():
        sorted (key), value in storedlist.items()  
        print (("{} --> {}").format(key, value))
Rutwb2
  • 147
  • 2
  • 2
  • 7
  • Is the surname the `key`? – enrico.bacis Apr 14 '15 at 16:31
  • *the methods I have seen all require the name of the dict*: You *have* the name of the dictionary: `storedlist`. – Martijn Pieters Apr 14 '15 at 16:32
  • @Oh right sorry, new to python haha and yes the first name and surname are the key, e.g. SAM WHITE – Rutwb2 Apr 14 '15 at 16:35
  • Could you show a few lines of the output your program generates so that we can see what the contents of the dictionaries look like? -- ok you answered that – mkrieger1 Apr 14 '15 at 16:36
  • I disagree that this is a duplicate question. The problem included to sort the data by the surnames, which is neither addressed in the linked question, nor in most of the answers here. – mkrieger1 Apr 14 '15 at 17:19

3 Answers3

13

Use the sorted keyword.

for key, value in sorted(storedlist.items()):
    # etc

How to sort dictionary by key in numerical order Python

https://wiki.python.org/moin/HowTo/Sorting

Community
  • 1
  • 1
Joe
  • 2,496
  • 1
  • 22
  • 30
2

Dictionaries are not ordered, so you have to sort the keys and then access the dictionary:

for key in sorted(storedlist.iterkeys()):
    print (("{} --> {}").format(key, storedlist[key]))
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
0

In order to sort by the surname of the students, you first have to extract that from the full name that is used as the dictionary key. The following function should be able to do that:

def get_surname(full_name):
    return full_name.split()[-1]

Then you can iterate over the dictionary contents like this:

for name in sorted(storedlist, key=get_surname):
    print("{} --> {}".format(name, storedlist[name]))

A few more tips:

  1. In your frq function you should replace skip with pass, which is a valid Python statement.

  2. Instead of defining several functions (clssa, clssb, clssc) which are identical except for using a different input filename, you can just write a single function which takes the filename as an argument (and give this function a better name):

    def print_scores(filename):
        f = open(filename, 'rb')
        storedlist = pickle.load(f)
        ...
    

    Inside the function clssq you would then replace

    if clss=='a':
        clssa()
    elif clss=='b':
        clssb()
    elif clss=='c':
        clssc()
    

    by

    filename = {'a': 'Class6A.txt',
                'b': 'Class6B.txt',
                'c': 'Class6C.txt'}[clss]
    print_scores(filename)
    
mkrieger1
  • 19,194
  • 5
  • 54
  • 65