acbart

Untitled

Dec 8th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. 1) Why is the school printing multiple times?
  3.  
  4. As you already know, the data returned by yelp.get_all_businesses() is a list of businesses, represented as a list of dictionaries. You're using the "schools" field of this dictionary (line 17) to get the schools associated with a business. However, a crucial thing to understand is that the abstraction associated with a businesses "schools" is the list of schools (coded as a list of strings), not a single school. For example, if you look at the 8th business ("Unique Pizza"), it has two schools that are associated with it!
  5.  
  6. >>> all_businesses[7]['name']
  7. "Unique Pizza"
  8. >>> all_businesses[7]['schools']
  9. ['Massachusetts Institute of Technology', 'Harvard University']
  10.  
  11. Originally, you appended each business['schools'] to the schoollist variable. This means that you end up with a list of lists of schools (most of which are only one school, but in the case of "Unique Pizza", has two schools!). My naive suggestion before was to use the extend function instead, which combines lists. Compare the following lines of code:
  12.  
  13. # Appending a list to another list
  14. >>> my_dogs = ['fido', 'spot', 'spike']
  15. >>> my_dogs.append(['spike', 'klaus'])
  16. >>> my_dogs
  17. ['fido', 'spot', 'spike', ['spike', 'klaus']]
  18. # Oh no, a list of strings and lists!
  19.  
  20. # Extending a list with another list
  21. >>> my_dogs = ['fido', 'spot', 'spike']
  22. >>> my_dogs.extend(['spike', 'klaus'])
  23. >>> my_dogs
  24. ['fido', 'spot', 'spike', 'spike', 'klaus']
  25. # A list of only strings, yay!
  26.  
  27. This would have worked fine, except you only care about unique schools. In order to make sure that you are only adding new schools, there is a convenient thing you can do in python after you have built your list of strings:
  28.  
  29. # http://stackoverflow.com/questions/7961363/python-removing-duplicates-in-lists?answertab=votes#tab-top
  30. >>> my_dogs = list(set(my_dogs))
  31. >>> my_dogs
  32. ['fido', 'spot', 'spike', 'klaus']
  33.  
  34. A set is a list that has no duplicates. So by passing in a list (such as schoollist) to the set function, and then converting that set back to a list, you remove all duplicates. We don't cover sets in this course, so let me know if this was confusing - there are other ways to accomplish this (a nested for loop) that might make more sense.
  35.  
  36. ------------------------------------------------------------------
  37. 2) Line 22's "Get a list of schools from the user" psuedo-code:
  38.  
  39. The following psuedo-code should map almost 1-1 with the correct python code:
  40.  
  41. all_choice = ask the user if they want to compare all colleges
  42. if all_choice is "yes"
  43. schoolnames = schoollist
  44. otherwise
  45. continue_choice = "yes"
  46. schoolnames is an empty list
  47. while continue_choice is "yes"
  48. school_choice = ask the user for their choice
  49. use difflib to make sure that the school_choice is in the list of schoollist
  50. if it is
  51. append the school_choice to the list of schoolnames
  52. continue_choice = ask the user if they want to continue
  53. optionally, check that the continue_choice was either "yes" or "no" and print out an error message if it isn't
  54. otherwise
  55. print out an error message, and let the loop continue again
  56.  
  57. ------------------------------------------------------------------
  58. 3) Line 62's "Show businesses with high reviews psuedo-code:
  59.  
  60. continue_choice = "yes"
  61. while continue_choice is "yes"
  62. school_choice = ask the user for their choice
  63. use difflib to make sure that the school is in the list of school names
  64. if it is
  65. for business in all_businesses:
  66. for school in business['schools']:
  67. if school == school_choice:
  68. if business['stars'] >= 4:
  69. print business['name']
  70. continue_choice = ask the user if they want to continue
  71. optionally, check that the continue_choice was either "yes" or "no" and print out an error message if it isn't
  72. otherwise
  73. print out an error message, and let the loop continue again
Advertisement
Add Comment
Please, Sign In to add comment