Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ddfe44

Browse files
committed
Less mutable state
1 parent 742adf1 commit 4ddfe44

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

fp_growth.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class FPTree(object):
8585
This object may only store transaction items that are hashable (i.e., all
8686
items must be valid as dictionary keys or set members).
8787
"""
88+
89+
Route = namedtuple('Route', 'head tail')
90+
8891
def __init__(self):
8992
# The root node of the tree.
9093
self._root = FPNode(self, None, None)
@@ -130,10 +133,10 @@ def _update_route(self, point):
130133
try:
131134
route = self._routes[point.item]
132135
route[1].neighbor = point # route[1] is the tail
133-
route[1] = point
136+
self._routes[point.item] = self.Route(route[0], point)
134137
except KeyError:
135138
# First node for this item; start a new route.
136-
self._routes[point.item] = [point, point]
139+
self._routes[point.item] = self.Route(point, point)
137140

138141
def items(self):
139142
"""
@@ -180,13 +183,13 @@ def _removed(self, node):
180183
# It was the sole node.
181184
del self._routes[node.item]
182185
else:
183-
self._routes[node.item][0] = node.neighbor
186+
self._routes[node.item] = self.Route(node.neighbor, tail)
184187
else:
185188
for n in self.nodes(node.item):
186189
if n.neighbor is node:
187190
n.neighbor = node.neighbor # skip over
188191
if node is tail:
189-
self._routes[node.item][1] = n
192+
self._routes[node.item] = self.Route(head, n)
190193
break
191194

192195
def conditional_tree_from_paths(paths, minimum_support):

0 commit comments

Comments
 (0)