fork download
  1. # your code goes here
  2.  
  3. def joinTable( tbl1, tbl2 ):
  4. op = []
  5. if len( tbl1 ) > 0 and len( tbl2 ) > 0:
  6. keys = set( tbl1[0].keys()).intersection( tbl2[0].keys())
  7. opkeys = set( tbl1[0].keys()).union( tbl2[0].keys())
  8. for row1 in tbl1:
  9. key1 = set( row1.keys())
  10. for row2 in tbl2:
  11. key2 = set( row2.keys())
  12. assert key1.intersection( key2 ) == keys
  13. assert key1.union( key2 ) == opkeys
  14. match = True
  15. for key in keys:
  16. match = row1[ key] == row2[key]
  17. if match:
  18. d = dict()
  19. for key in opkeys:
  20. d[ key ] = row1[ key ] if key in row1 else row2[key]
  21. op.append( d )
  22. return op
  23.  
  24. print joinTable( [{'s_id': 'bb', 'title': 'Breaking Bad'},{'s_id': 'bcs', 'title': 'Better Call Saul'}],[
  25. {'c_id': 'ww', 's_id': 'bb'},{'c_id': 'sw', 's_id': 'bb'}, {'c_id': 'sg', 's_id': 'bb'},{'c_id': 'sg', 's_id': 'bcs'}])
  26.  
  27.  
Success #stdin #stdout 0s 9024KB
stdin
Standard input is empty
stdout
[{'s_id': 'bb', 'title': 'Breaking Bad', 'c_id': 'ww'}, {'s_id': 'bb', 'title': 'Breaking Bad', 'c_id': 'sw'}, {'s_id': 'bb', 'title': 'Breaking Bad', 'c_id': 'sg'}, {'s_id': 'bcs', 'title': 'Better Call Saul', 'c_id': 'sg'}]