user9562553
user9562553

Reputation:

How to create a key that identifies a set of numbers in order to retrieve a set in a collection if we do not have a key?

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

Answers (1)

Alex Hall
Alex Hall

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

Related Questions