for i in range(4): for j in range(6): print i,j 0 0 0 1 0 2 0 3 0 4 0 5 1 0 1 1 1 2 1 3 1 4 1 5 2 0 2 1 2 2 2 3 2 4 2 5 3 0 3 1 3 2 3 3 3 4 3 5 for i in range(4): for j in range(i+1,6): print i,j 0 1 0 2 0 3 0 4 0 5 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 lst = [("x",4,"y"), ("a",8,"g"), ("e", 2, "f")] import operator x = sorted(lst,key=operator.itemgetter(2)) print x [('e', 2, 'f'), ('a', 8, 'g'), ('x', 4, 'y')] y = sorted(lst,key=operator.itemgetter(0,2)) print y [('a', 8, 'g'), ('e', 2, 'f'), ('x', 4, 'y')] lst.append(("e",3,"a")) print lst [('x', 4, 'y'), ('a', 8, 'g'), ('e', 2, 'f'), ('e', 3, 'a')] y = sorted(lst,key=operator.itemgetter(0,2)) print y [('a', 8, 'g'), ('e', 3, 'a'), ('e', 2, 'f'), ('x', 4, 'y')] y = sorted(lst,key=operator.itemgetter(0,2), reverse=True) print y [('x', 4, 'y'), ('e', 2, 'f'), ('e', 3, 'a'), ('a', 8, 'g')] # below is the output from running SimpleFoodReader. # Although items looks like a list of strings, note its type is a string, # not a list. It is a string that looks like a list of strings! # Also note that ratings looks like a dictionary, but its type is also # a string, a string that looks like a dictionary! # We then use JSON to convert items into the list of strings named var, # and to convert ratings into the dictionary named dvar. # items = ["Nasher Cafe", "ABP", "WaDuke", "Loop", "Panda", "Penn Pavilion", "McDonalds", "Blue Express"] ratings = {"student1004": [0, 1, 0, 0, 1, 0, 5, 0], "student1001": [3, 3, -3, 5, 3, 1, 0, 0], "student1003": [5, -3, 3, 0, 3, 0, 3, -3], "student1002": [0, 1, 5, -3, 1, 0, 5, -3]} # note the type of items and ratings next! #after applying json.load, var is now a list. Ignore the u in front of # each string, that just means its unicode. [u'Nasher Cafe', u'ABP', u'WaDuke', u'Loop', u'Panda', u'Penn Pavilion', u'McDonalds', u'Blue Express'] #after applying json.load, dvar is now a dictionary. Again ignore the u that # appears before each string. {u'student1004': [0, 1, 0, 0, 1, 0, 5, 0], u'student1001': [3, 3, -3, 5, 3, 1, 0, 0], u'student1003': [5, -3, 3, 0, 3, 0, 3, -3], u'student1002': [0, 1, 5, -3, 1, 0, 5, -3]}