fork(1) download
  1. # more_about_dictionaries.py
  2. # MIT OCW 6.189 Homework 3
  3. # http://o...content-available-to-author-only...t.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw3.pdf
  4. # Written for Python 3.x
  5. # Mechanical MOOC
  6. # Glenn Richard
  7. # July 20, 2013
  8. # Exercise 3.4 - More About Dictionaries
  9. NAMES = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank',
  10. 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']
  11. AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
  12.  
  13. def combine_lists(names_list, ages_list):
  14. return {k: v for k, v in zip(names_list, ages_list)}
  15.  
  16. def people(age):
  17. return [name for name in people_dict.keys() if people_dict[name] == age]
  18.  
  19. people_dict = combine_lists(NAMES, AGES)
  20.  
  21. print('Dan' in people(18) and 'Cathy' in people(18))
  22. print('Ed' in people(19) and 'Helen' in people(19) and 'Irene' in people(19) and 'Jack' in people(19) and 'Larry'in people(19))
  23. print('Alice' in people(20) and 'Frank' in people(20) and 'Gary' in people(20))
  24. print(people(21) == ['Bob'])
  25. print(people(22) == ['Kelly'])
  26. print(people(23) == [])
  27.  
Success #stdin #stdout 0.15s 10248KB
stdin
Standard input is empty
stdout
True
True
True
True
True
True