Posts

Showing posts from December 13, 2016

Python Sorting Part 2: Classes and Attributes

Python Sorting with Classes and Attributes # Here we define a class, containing student names # each student contains an list attribute of varying from 1 to 7 in length # populated by random integer values from 0 to 500 import random # imports the random module # declares the class object class myClass(object):     def __init__(self, inputName):          self.myName = inputName # creates a myName attribute         # create a list of random length         self.myList = [random.randint(0,500) \                         for x in range(random.randint(1, 7))]              def greet(self):         # prints out name and list of values the instance holds         print 'Hi I am {}, my list is {}'.format(self.myName, self.myList) random.seed(0) # providing a seed to genera...

Python Sorting Part 1: Dictionary Keys

Sorting Python Dictionary Keys Recently I ran into the need to perform more involved operations on Python dictionaries and sorting of ordered lists. This led me to a deeper understanding of a side of the sorted() function that I used to shy away from.  The biggest area that I had to learn about was sorting values by a key -- how it works and the practical applications. So the following lines of code are my 'journal' in this little learning journey through dictionary operations and sorting.  # Here's a dictionary d that has strings as keys, and a list of integers for each key's value. d = {'m': [23, 42, 63], 'm800': [32, 53, 743, 8], 'm23': [3324,425,21], 'a132': [2, 2, 53, 64]} # Here's a dictionary  e  that has integers as keys, and a list of integers for each key's value. e = {1: [42, 43, 52], 200: [3, 53, 63, 2], 60: [4, 62, 96], 30: [63, 89], 500: [32]} print d # Result: {'a132': [2, 2, 53, 64], 'm8...