Advertisement
proffreda

Exercise in Basic Python data structures - Lists and Dicts

Jun 22nd, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. # HW Exercise in Basic Python data structures - Lists and Dicts
  2. # Reproduce the following python interactive listing using different data of your choosing, e.g., choose different variable names and
  3. # data strings other than ‘Bob Smith’ and ‘Sue Jones’
  4.  
  5. >>> bob = ['Bob Smith', 42, 30000, 'software']
  6. >>> sue = ['Sue Jones', 45, 40000, 'hardware']
  7.  
  8. >>> bob[0], sue[2] # fetch name, pay
  9. ('Bob Smith', 40000)
  10.  
  11. >>> bob[0].split()[-1] # what's bob's last name?
  12. 'Smith'
  13.  
  14. >>> sue[2] *= 1.25 # give sue a 25% raise
  15. >>> sue
  16.  
  17. ['Sue Jones', 45, 50000.0, 'hardware']
  18.  
  19. >>> people = [bob, sue ] # reference in list of lists
  20.  
  21. >>> for person in people:
  22. print(person)
  23.  
  24. ['Bob Smith', 42, 30000, 'software']
  25. ['Sue Jones', 45, 50000.0, 'hardware']
  26.  
  27. >>> people[1][0]
  28.  
  29. 'Sue Jones'
  30.  
  31. >>> for person in people:
  32. print(person[0].split()[-1]) # print last names
  33. person[2] *= 1.20 # give each a 20% raise
  34.  
  35. Smith
  36. Jones
  37.  
  38. >>> for person in people: print(person[2]) # check new pay
  39.  
  40. 36000.0
  41. 60000.0
  42.  
  43. >>> pays = [person[2] for person in people] # collect all pay
  44. >>> pays
  45.  
  46. [36000.0, 60000.0]
  47.  
  48. >>> pays = map((lambda x: x[2]), people) # ditto (map is a generator in 3.X)
  49.  
  50. >>> list(pays)
  51.  
  52. [36000.0, 60000.0]
  53. >>> sum(person[2] for person in people) # generator expression, sum built-in
  54.  
  55. 96000.0
  56.  
  57. >>> people.append(['Tom', 50, 0, None])
  58. >>> len(people)
  59.  
  60. 3
  61.  
  62. >>> people[-1][0]
  63.  
  64. 'Tom'
  65.  
  66. >>> NAME, AGE, PAY = range(3) # 0, 1, and 2
  67. >>> bob = ['Bob Smith', 42, 10000]
  68. >>> bob[NAME]
  69.  
  70. 'Bob Smith'
  71.  
  72. >>> PAY, bob[PAY]
  73.  
  74. (2, 10000)
  75.  
  76. >>> bob = [['name', 'Bob Smith'], ['age', 42], ['pay', 10000]]
  77. >>> sue = [['name', 'Sue Jones'], ['age', 45], ['pay', 20000]]
  78. >>> people = [bob, sue]
  79.  
  80. >>> for person in people:
  81. print(person[0][1], person[2][1]) # name, pay
  82.  
  83. Bob Smith 10000
  84. Sue Jones 20000
  85.  
  86. >>> [person[0][1] for person in people] # collect names
  87.  
  88. ['Bob Smith', 'Sue Jones']
  89.  
  90. >>> for person in people:
  91. print(person[0][1].split()[-1]) # get last names
  92. person[2][1] *= 1.10 # give a 10% raise
  93.  
  94. Smith
  95. Jones
  96.  
  97. >>> for person in people: print(person[2])
  98.  
  99. ['pay', 11000.0]
  100. ['pay', 22000.0]
  101.  
  102. >>> for person in people:
  103. for (name, value) in person:
  104. if name == 'name': print(value) # find a specific field
  105.  
  106. Bob Smith
  107. Sue Jones
  108.  
  109. >>> def field(record, label):
  110. for (fname, fvalue) in record:
  111. if fname == label: # find any field by name
  112. return fvalue
  113.  
  114. >>> field(bob, 'name')
  115. 'Bob Smith'
  116.  
  117. >>> field(sue, 'pay')
  118. 22000.0
  119.  
  120. >>> for rec in people:
  121. print(field(rec, 'age')) # print all ages
  122.  
  123. 42
  124. 45
  125.  
  126. >>> bob = {'name': 'Bob Smith', 'age': 42, 'pay': 30000, 'job': 'dev'}
  127. >>> sue = {'name': 'Sue Jones', 'age': 45, 'pay': 40000, 'job': 'hdw'}
  128.  
  129.  
  130. >>> bob['name'], sue['pay'] # not bob[0], sue[2]
  131.  
  132. ('Bob Smith', 40000)
  133.  
  134. >>> bob['name'].split()[-1]
  135.  
  136. 'Smith'
  137.  
  138. >>> sue['pay'] *= 1.10
  139. >>> sue['pay']
  140.  
  141. 44000.0
  142.  
  143. >>> bob = dict(name='Bob Smith', age=42, pay=30000, job='dev')
  144. >>> sue = dict(name='Sue Jones', age=45, pay=40000, job='hdw')
  145.  
  146.  
  147. >>> bob
  148.  
  149. {'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
  150.  
  151. >>> sue
  152.  
  153. {'pay': 40000, 'job': 'hdw', 'age': 45, 'name': 'Sue Jones'}
  154.  
  155. >>> sue = {}
  156. >>> sue['name'] = 'Sue Jones'
  157. >>> sue['age'] = 45
  158. >>> sue['pay'] = 40000
  159. >>> sue['job'] = 'hdw'
  160. >>> sue
  161.  
  162. {'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
  163.  
  164.  
  165. >>> names = ['name', 'age', 'pay', 'job']
  166. >>> values = ['Sue Jones', 45, 40000, 'hdw']
  167. >>> list(zip(names, values))
  168.  
  169. [('name', 'Sue Jones'), ('age', 45), ('pay', 40000), ('job', 'hdw')]
  170.  
  171. >>> sue = dict(zip(names, values))
  172. >>> sue
  173.  
  174. {'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
  175.  
  176. >>> fields = ('name', 'age', 'job', 'pay')
  177. >>> record = dict.fromkeys(fields, '?')
  178. >>> record
  179.  
  180. {'job': '?', 'pay': '?', 'age': '?', 'name': '?'}
  181.  
  182. >>> bob
  183.  
  184. {'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
  185.  
  186. >>> sue
  187.  
  188. {'job': 'hdw', 'pay': 40000, 'age': 45, 'name': 'Sue Jones'}
  189.  
  190. >>> people = [bob, sue] # reference in a list
  191. >>> for person in people:
  192. print(person['name'], person['pay'], sep=', ') # all pay
  193.  
  194. Bob Smith, 30000
  195. Sue Jones, 40000
  196.  
  197. >>> for person in people:
  198. if person['name'] == 'Sue Jones': # fetch sue's pay
  199. print(person['pay'])
  200.  
  201. 40000
  202.  
  203. >>> names = [person['name'] for person in people] # collect names
  204. >>> names
  205.  
  206. ['Bob Smith', 'Sue Jones']
  207.  
  208. >>> list(map((lambda x: x['name']), people)) # ditto, generate
  209.  
  210. ['Bob Smith', 'Sue Jones']
  211.  
  212. >>> sum(person['pay'] for person in people) # sum all pay
  213.  
  214. 70000
  215.  
  216. >>> for person in people:
  217. print(person['name'].split()[-1]) # last name
  218. person['pay'] *= 1.10 # a 10% raise
  219.  
  220. Smith
  221. Jones
  222.  
  223. >>> for person in people: print(person['pay'])
  224.  
  225. 33000.0
  226. 44000.0
  227.  
  228. >>> bob2 = {'name': {'first': 'Bob', 'last': 'Smith'},
  229. 'age': 42,
  230. 'job': ['software', 'writing'],
  231. 'pay': (40000, 50000)}
  232.  
  233. >>> bob2['name'] # bob's full name
  234.  
  235. {'last': 'Smith', 'first': 'Bob'}
  236.  
  237. >>> bob2['name']['last'] # bob's last name\
  238.  
  239. 'Smith'
  240.  
  241. >>> bob2['pay'][1] # bob's upper pay
  242.  
  243. 50000
  244.  
  245. >>> for job in bob2['job']: print(job) # all of bob's jobs
  246.  
  247. software
  248. writing
  249.  
  250. >> bob2['job'][-1] # bob's last job
  251.  
  252. 'writing'
  253.  
  254. >>> bob2['job'].append('janitor') # bob gets a new job
  255.  
  256. >>> bob2
  257.  
  258. {'job': ['software', 'writing', 'janitor'], 'pay': (40000, 50000), 'age': 42, 'name':
  259.  
  260. {'last': 'Smith', 'first': 'Bob'}}
  261.  
  262. >>> bob = dict(name='Bob Smith', age=42, pay=30000, job='dev')
  263. >>> sue = dict(name='Sue Jones', age=45, pay=40000, job='hdw')
  264. >>> bob
  265.  
  266. {'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
  267.  
  268. >>> db = {}
  269. >>> db['bob'] = bob # reference in a dict of dicts
  270. >>> db['sue'] = sue
  271. >>> db['bob']['name'] # fetch bob's name
  272.  
  273. 'Bob Smith'
  274.  
  275. >>> db['sue']['pay'] = 50000 # change sue's pay
  276. >>> db['sue']['pay'] # fetch sue's pay
  277.  
  278. 50000
  279.  
  280. >>> db
  281.  
  282. {'bob': {'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}, 'sue': {'pay': 50000, 'job': 'hdw', 'age': 45, 'name': 'Sue Jones'}}
  283.  
  284. >>> import pprint
  285. >>> pprint.pprint(db)
  286.  
  287. {'bob': {'age': 42, 'job': 'dev', 'name': 'Bob Smith', 'pay': 30000},
  288. 'sue': {'age': 45, 'job': 'hdw', 'name': 'Sue Jones', 'pay': 50000}}
  289.  
  290.  
  291. >>> for key in db:
  292. print(key, '=>', db[key]['name'])
  293.  
  294. bob => Bob Smith
  295. sue => Sue Jones
  296.  
  297. >>> for key in db:
  298. print(key, '=>', db[key]['pay'])
  299.  
  300. bob => 30000
  301. sue => 50000
  302.  
  303. >>> for key in db:
  304. print(db[key]['name'].split()[-1])
  305. db[key]['pay'] *= 1.10
  306.  
  307. Smith
  308. Jones
  309.  
  310. >>> for record in db.values(): print(record['pay'])
  311.  
  312. 33000.0
  313. 55000.0
  314.  
  315. >>> x = [db[key]['name'] for key in db]
  316. >>> x
  317.  
  318. ['Bob Smith', 'Sue Jones']
  319.  
  320. >>> x = [rec['name'] for rec in db.values()]
  321. >>> x
  322.  
  323. ['Bob Smith', 'Sue Jones']
  324.  
  325. >>> db['tom'] = dict(name='Tom', age=50, job=None, pay=0)
  326. >>> db['tom']
  327.  
  328. {'pay': 0, 'job': None, 'age': 50, 'name': 'To'}
  329.  
  330. >>> db['tom']['name']
  331.  
  332. 'Tom'
  333.  
  334. >>> list(db.keys())
  335.  
  336. ['bob', 'sue', 'tom']
  337.  
  338. >>> len(db)
  339.  
  340. 3
  341.  
  342. >>> [rec['age'] for rec in db.values()]
  343.  
  344. [42, 45, 50]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement