+ 7

About x+=2 and x = x + 2

I used to like the compactness of the expression, like using x += 2 instead of x = x + 2. But, now I am learning Hamiltonian Path, or Graph Theory. This is my graph dictionary: g = {'a':['c'], 'b':['c','e','f'], 'c':['a','b','d','e'], 'd':['c'], 'e':['b','c','f'], 'f':['b','e'] } According to the dict, each key is able to access to one of their element in the list. So it form a graph I like to find all paths which are from node 'a' to 'f'. In the class Graph: I define a method call 'find_all_paths' which takes a start_vertex, end_vertex, and a path in the parentheses. This is my method's code, and please pay attention to the third line which have += . def find_all_paths(self, start_vertex, end_vertex, path = []): graph = self.graph_dict path += [start_vertex] if start_vertex == end_vertex: return [path] if start_vertex not in graph: return [] paths = [] for vertex in graph[start_vertex]: if vertex not in path: extended_path = self.find_all_paths(vertex, end_vertex, path) for p in extended_path: paths.append(p) return paths Using this to find all paths from 'a' to 'f', I get [['a', 'c', 'b', 'e', 'f', 'd']] But now I test this but changing third line to path = path + [start_index] instead using +=: I get[['a', 'c', 'b', 'e', 'f'], ['a', 'c', 'b', 'f'], ['a', 'c', 'e', 'b', 'f'], ['a', 'c', 'e', 'f']] which is the expected answer. What wrong have I done? Is there any explanation about why += cannot form the same output?

2nd Dec 2017, 10:55 AM
kelvin hong 方
kelvin hong 方 - avatar
6 odpowiedzi
+ 2
(3) I doubt in a few point: 1) I am using Window XP, didn't renew since about 10 years ago, is it a reason? 2)I using Sololearn's Code Playground, Python, is there any difference with executing these code in other Python template? 3)As in 'path += [start_vertex]', I want the output starts with 'a' and ends with 'f', but the output '[['a', 'c', 'b', 'e', 'f', 'd']]' is even didn't ends with 'f', and the other code done it as well.
2nd Dec 2017, 1:32 PM
kelvin hong 方
kelvin hong 方 - avatar
+ 2
(1) Not to waste your time, I put my code here. Let's check! This is 'path = path + [start_vertex]' version: class Graph: def __init__ (self, graph_dict = None): if graph_dict == None: self.graph_dict = {} else: self.graph_dict = graph_dict def find_all_paths(self, start_vertex, end_vertex, path = []): graph = self.graph_dict path = path + [start_vertex] if start_vertex == end_vertex: return [path] if start_vertex not in graph: return [] paths = [] for vertex in graph[start_vertex]: if vertex not in path: extended_path = self.find_all_paths(vertex, end_vertex, path) for p in extended_path: paths.append(p) return paths g = {'a':['c'], 'b':['c','e','f'], 'c':['a','b','d','e'], 'd':['c'], 'e':['b','c','f'], 'f':['b','e'] } graph = Graph(g) print (graph.find_all_paths('a','f')) Output: [['a', 'c', 'b', 'e', 'f'], ['a', 'c', 'b', 'f'], ['a', 'c', 'e', 'b', 'f'], ['a', 'c', 'e', 'f']]
2nd Dec 2017, 1:32 PM
kelvin hong 方
kelvin hong 方 - avatar
+ 2
(2) This is 'path += [start_vertex]' version, I am just change that line, remain others the same: class Graph: def __init__ (self, graph_dict = None): if graph_dict == None: self.graph_dict = {} else: self.graph_dict = graph_dict def find_all_paths(self, start_vertex, end_vertex, path = []): graph = self.graph_dict path += [start_vertex] if start_vertex == end_vertex: return [path] if start_vertex not in graph: return [] paths = [] for vertex in graph[start_vertex]: if vertex not in path: extended_path = self.find_all_paths(vertex, end_vertex, path) for p in extended_path: paths.append(p) return paths g = {'a':['c'], 'b':['c','e','f'], 'c':['a','b','d','e'], 'd':['c'], 'e':['b','c','f'], 'f':['b','e'] } graph = Graph(g) print (graph.find_all_paths('a','f')) Output: [['a', 'c', 'b', 'e', 'f', 'd']]
2nd Dec 2017, 1:33 PM
kelvin hong 方
kelvin hong 方 - avatar
+ 2
you lost me at x+=2 😦
5th Dec 2017, 4:41 AM
Mason Neville
Mason Neville - avatar
+ 2
You can try my code , I just bought a new laptop and tried on it , the problem remains...
5th Dec 2017, 4:52 AM
kelvin hong 方
kelvin hong 方 - avatar
+ 1
This shouldn't be the case! += is just an alias for _=_+()
2nd Dec 2017, 12:53 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar