I am now trying to extend my program to allow the events in the file to be sorted by any of the fields (date, time, title, description, location and to have an optional list of attendees) Whether the attendees are present or not I need to allow either an empty list or a list of their names to be displayed.

I have tried implementing some code to allow the fields inside the file to be sorted but I am getting confused as I am still very new to Python. Please could someone help.

Here is my code:

import cPickle
def print_menu():
    print '1. Add an Event'
    print '2. Save Events to File'
    print '3. Load Events from File'
    print '4. Sort Events'
    print '5. Quit'
    print ""
event_list = []
menu_choice = 0
start = 0
tmp = []
while True:
    menu_choice = input("Select Menu Item (1-4): ")
    if menu_choice == 1:
            print "Add Event"
            date = raw_input("Enter Date (yyyy,mm,dd): ")
            # Assuming that the user inputs this: yyyy,mm,dd
            date = date.split(',')
            if len(date[0]) == 2:
                date[0] = '20'+date[0]
            if len(date[1]) == 1:
                date[1] = '0'+date[1]
            if len(date[2]) == 1:
                date[2] = '0'+date[2]
            date = '-'.join(date)
            # if the format yyyy,mm,dd is not entered
        except IndexError:
            print "That was not in the format yyyy,mm,dd"
            start # restarts the loop
        time = raw_input("Time: ")
        title = raw_input("Title: ")
        desc = raw_input("Description: ")
        loc = raw_input("Location: ")
        #attendop = raw_input("Include Attendees(Y/N)?")
        #if attendop = y:
        attend = raw_input("Attendee(s): ")
        #elif attendop = n:
            #ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc + '\n'
            #attendop = raw_input("Enter Y or N")
        ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
    elif menu_choice == 2:
        filename = raw_input("Filename to save: ")
        pickle_file = open(filename, "w")
        cPickle.dump(event_list, pickle_file)
    elif menu_choice == 3:
            filename = raw_input("Filename to load: ")
            pickle_file = open(filename, "r")
            events = cPickle.load(pickle_file)    # events is a list of events
            sortitem = raw_input("Sort By.. (Enter date, time, title, desc, loc or attendee):")
            if sortitem == date:
                for line in events:
                    line = line[:-1]
                    bits = line.split('\t')
                    folks = eval(bits[0])

                results = []
                for line in tmp:
                    print str(results)+"\n"
            #for event in events:                  # print each member of events
                #print "Events: %s" % '\t'.join(list(event))
        except IOError:
            print "No such file to load"
    elif menu_choice == 5:
print "Goodbye"

the parts I have been testing for the optional attendees I have commented out as I was unable to run my program to test the sorting due to errors.

Thank You in advance!

You can sort a list of lists on any element using the operator itemgetter

import operator

test_list = [ ["a", "b", "c", "d", "e"],
              ["e", "d", "c", "b", "a"],
              ["c", "d", "e", "a", "b"] ]

for j in range(0, 5):
    print j
    s_list = sorted(test_list, key=operator.itemgetter(j))
    print s_list

Edited 6 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.