Hash function for sets of IDs

44 Views Asked by At

Is there a hash algorithm/function in Python that is able to convert sets of unique strings to a single hash string?

For example, a set of {a,b,c} would return some unique ID, the same unique ID as for {c,a,b} or {b,c,a} etc.

1

There are 1 best solutions below

0
Stef On BEST ANSWER

There is the builtin hash function for this purpose. Two frozensets containing the same values are guaranteed to have the same hash.

>>> s=frozenset(('hello','abc','12','42','zyx'))
>>> t=frozenset(('zyx','hello','abc','42','12'))
>>> s
frozenset({'abc', '42', '12', 'hello', 'zyx'})
>>> t
frozenset({'abc', '42', 'hello', '12', 'zyx'})
>>> hash(s)
8290365720384972439
>>> hash(t)
8290365720384972439
>>> hash(s)==hash(t)
True