Sort of
math.sqrt(sum((a[k] - b[k])**2 for k in a.keys()))
Where a and b are dictionaries with the same keys. If you are going to compare these values ββbetween different pairs of vectors, then you must make sure that each vector contains exactly the same words, otherwise your distance measure does not mean anything.
You can calculate the distance based only on the intersection:
math.sqrt(sum((a[k] - b[k])**2 for k in set(a.keys()).intersection(set(b.keys()))))
Another option is to use union and set unknown values ββto 0
math.sqrt(sum((a.get(k, 0) - b.get(k, 0))**2 for k in set(a.keys()).union(set(b.keys()))))
But you should carefully think about what it really means that you are counting.
source share