К размышлению:
from sympy import *
x, y, z = symbols('x y z')
a = x+x+x+y+z
b = y+x+z+x+x
print simplify(a)
print simplify(b)
>>> 
3*x + y + z
3*x + y + z
>>>