Hash join: Difference between revisions

added python
(added ocaml)
(added python)
Line 240:
[[2, "Alan"], ["Alan", "Zombies"]]
[[3, "Glory"], ["Glory", "Buffy"]]</pre>
 
=={{header|Python}}==
<lang python>from collections import defaultdict
 
def hashJoin(table1, table2):
h = defaultdict(list)
# hash phase
for s in table1:
k = s[1]
h[k].append(s)
# join phase
result = []
for r in table2:
k = r[0]
for s in h[k]:
result.append((s, r))
return result
 
table1 = [(1, "Jonah"),
(2, "Alan"),
(3, "Glory"),
(4, "Popeye")]
table2 = [("Jonah", "Whales"),
("Jonah", "Spiders"),
("Alan", "Ghosts"),
("Alan", "Zombies"),
("Glory", "Buffy")];
 
for row in hashJoin(table1, table2):
print(row)</lang>
{{out}}
<pre>
((1, 'Jonah'), ('Jonah', 'Whales'))
((1, 'Jonah'), ('Jonah', 'Spiders'))
((2, 'Alan'), ('Alan', 'Ghosts'))
((2, 'Alan'), ('Alan', 'Zombies'))
((3, 'Glory'), ('Glory', 'Buffy'))
</pre>
 
=={{header|Tcl}}==
Anonymous user