Reputation:
I have several sets of numbers. I want to store these sets in a dictionary. I don't know how to generate a key that identifies the set in a unique way. In the case where I have a set without key, I want to generate automatically a key for that set and check if the generated key is in the dictionary.
Upvotes: 3
Views: 53
Reputation: 36033
You can use a frozenset
as a dictionary key:
d = {frozenset([1, 2, 3]): 'a', frozenset([4, 5, 6]): 'b'}
print(d[frozenset([1, 2, 3])]) # 'a'
Upvotes: 3