Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
23
Task/Hash-join/Python/hash-join.py
Normal file
23
Task/Hash-join/Python/hash-join.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from collections import defaultdict
|
||||
|
||||
def hashJoin(table1, index1, table2, index2):
|
||||
h = defaultdict(list)
|
||||
# hash phase
|
||||
for s in table1:
|
||||
h[s[index1]].append(s)
|
||||
# join phase
|
||||
return [(s, r) for r in table2 for s in h[r[index2]]]
|
||||
|
||||
table1 = [(27, "Jonah"),
|
||||
(18, "Alan"),
|
||||
(28, "Glory"),
|
||||
(18, "Popeye"),
|
||||
(28, "Alan")]
|
||||
table2 = [("Jonah", "Whales"),
|
||||
("Jonah", "Spiders"),
|
||||
("Alan", "Ghosts"),
|
||||
("Alan", "Zombies"),
|
||||
("Glory", "Buffy")]
|
||||
|
||||
for row in hashJoin(table1, 1, table2, 0):
|
||||
print(row)
|
||||
Loading…
Add table
Add a link
Reference in a new issue