This code could be more concise.

- Put every number into a
**two-layer dictionary**to record it's multiply. - For each query pair, check whether they're in the dictionary or not.

If both exist => solution must exist. - Record the multiply and the number we've visited already, keep seeking the dictionary until find out the solution.

For example:

equation = [["a","e"],["b","e"]], value = [4,3], query = [["a", "b"]]

- Construct a dictionary =
**{"a": {"a":1, "e":4}, "b": {"b":1, "e":3}, "e": {"a": 1/4, "b": 1/3} }** - Follow the path: dct["a"] => dct["e"] => dct["b"], we have query [["a", "b"]] =
**4 x 1/3**= 4/3

```
class Solution(object):
def calcEquation(self, equations, values, query):
def seeker(a, b, path=[]):
# seek the result of a/b
if a not in dct.keys() or b not in dct.keys(): # No solution
return 0
if b in dct[a]: # This is it!
return dct[a][b]
else: # Keep looking for solution
tmp = []
for c in dct[a].keys():
if c not in path and (seeker(c, b, path+[c])):
return dct[a][c]*(seeker(c, b, path+[c]))
dct = {} # Put every number into the dict
for i in xrange(len(equations)):
nums = equations[i]
div = float(values[i])
if nums[0] in dct.keys():
dct[nums[0]][nums[1]] = div
else:
dct[nums[0]] = {nums[0]:1, nums[1]:div}
if nums[1] in dct.keys():
dct[nums[1]][nums[0]] = 1.0/div
else:
dct[nums[1]] = {nums[1]:1, nums[0]:1.0/div}
res = []
for pair in query: # seek the solution
if seeker(pair[0], pair[1]):
res += seeker(pair[0], pair[1]),
else:
res += -1,
return [float(n) for n in res]
```

**EDIT:**

Thanks to @WKVictor 's simplification, it improved from 45ms -> 32ms.