Advertisement
Darryl-Medley

Index Sort

May 14th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. # In-memory Database Index Sort Demo program
  2. # Written on 05/13/2014 by Darryl Medley for Coding 101 on the TWiT network
  3.  
  4. # Generic String List print routine
  5. def printList(nmList):
  6.     for rec in nmList:
  7.         print ','.join(rec)   # ','.join converts each sub-list into a comma-delimited string
  8.  
  9. # Begin main program    
  10. print "Coding 101 In-memory Database Index Sort Demo Program\n"
  11. fname = "./name_list.txt"
  12. try:
  13.     # hard-coded version of what is normally read from a file
  14.     infoList = ["padre, 30, 90000","Shannon, 21, 93000","Darryl, 50, 93000","Timmy, 9, 92000"]
  15.        
  16. # Activate this code to read the list from the name_list.txt file used for the ReadSortWrite.py program
  17. ##    with open(fname, "r") as nameListFile:  # open the text file to read
  18. ##        # Read the file into a Python list. Each line in the file becomes a
  19. ##        # string element in the list. .splitlines() removes the \n (newline)
  20. ##        # character from the end of each line and filter(lamda...) skips blank lines
  21. ##        infoList = filter(lambda r : r != '',nameListFile.read().splitlines())
  22.        
  23. except:
  24.     print "Error: File",fname,"does not exist"
  25. else:
  26.     # Convert each comma-delimited string in the list into a sub-list
  27.     # and remove any leading and trailing blanks (whitespace)
  28.     infoList = [[fld.strip() for fld in s.split(",")] for s in infoList]
  29.  
  30.     # Create index lists for each field we want to sort by. Each index list is a list of
  31.     # two-element sub-lists where the first element is the field we are sorting by (the "key")
  32.     # and the second element is the index # of the entry in main list the key is from.
  33.     #
  34.     # Make the Name index upper case so it case-insensitive
  35.     nameIdxLst = sorted([[fld[0].upper(), i] for i, fld in enumerate(infoList)])
  36.     # Convert the Age to a number so values like 9 sort less than 10
  37.     ageIdxLst = sorted([[int(fld[1]), i] for i, fld in enumerate(infoList)])
  38.     # Since Zipcodes are so common we append the name to the key to subsort by name
  39.     zipIdxLst = sorted([[fld[2]+fld[0], i] for i, fld in enumerate(infoList)])
  40.  
  41.     print "Unsorted List (Name, Age, Zipcode):"
  42.     printList(infoList)   # call our list print function defined above
  43.    
  44.     # Print the main list sorted 3 different ways side-by-side using our index lists
  45.     print "\nSorted Lists:\n"
  46.     print "{:20}{:20}{:20}".format("By Name:","By Age:","By Zipcode:")
  47.     for i in range(len(infoList)):
  48.         print "{:20}{:20}{:20}".format(','.join(infoList[nameIdxLst[i][1]]),
  49.                                        ','.join(infoList[ageIdxLst[i][1]]),
  50.                                        ','.join(infoList[zipIdxLst[i][1]]))
  51.  
  52. raw_input("\n(press Enter to close)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement