I have set, some_set = {'A', 'B', 'C'}
I'm looking to turn this into a dictionary like so:
my_dict = {'A':{'B','C'}, 'B':{'A','C'}, 'C':{'A','B'}}
ie a dictionary of sets, where every key is mapped to a set having all elements from the original set except the key itself.
What I have right now:
for letter_1 in some_set
for letter_2 in some_set:
if letter_1 not in my_dict.keys():
my_dict[letter_1] = set()
if letter_2 != letter_1:
my_dict[letter_1].add(letter_2)
What I'm looking for: Is this the best way to do it? Would there be a more pythonic way to achieve what I want?
A simple dictionary comprehension:
>>> some_set = {'A', 'B', 'C'}
>>> my_dict = {k: some_set - {k} for k in some_set}
{'A': {'B', 'C'}, 'B': {'A', 'C'}, 'C': {'A', 'B'}}
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With