Open essepuntato opened 4 years ago
def set_organize(input_set):
set_hobbit = set()
set_magician = set()
output_dict = dict()
output_dict["hobbit"] = set_hobbit
output_dict["magician"] = set_magician
for element in input_set:
if element in set({"Frodo", "Pippin","Merry", "Sam"}):
set_hobbit.add(element)
if element in set({"Gandalf", "Saruman"}):
set_magician.add(element)
return output_dict
tolkien_dict = set_organize(my_set)
print(tolkien_dict)
# it prints the dictionary {'hobbit': {'Frodo', 'Sam', 'Pippin', 'Merry'}, 'magician': {'Saruman', 'Gandalf'}}
my_dict = {"hobbit": {"Frodo", "Sam", "Pippin", "Merry"}, "magician": {"Saruman", "Gandalf"}}
set_hobbit = {"Frodo", "Sam", "Pippin", "Merry"}
set_magician = {"Saruman", "Gandalf"}
LoTR_dictionary = dict()
LoTR_dictionary["hobbit"] = set_hobbit
LoTR_dictionary["magician"] = set_magician
print(LoTR_dictionary)
==============================================================================================
{'hobbit': {'Merry', 'Sam', 'Frodo', 'Pippin'}, 'magician': {'Saruman', 'Gandalf'}}
==============================================================================================
set_hobbit = {"Frodo", "Sam", "Pippin", "Merry"}
set_magician = {"Saruman", "Gandalf"}
my_dictionary = {"hobbit": set_hobbit, "magician": set_magician}
print(my_dictionary)
# --> {'hobbit': {'Pippin', 'Frodo', 'Sam', 'Merry'}, 'magician': {'Gandalf', 'Saruman'}}
{'hobbit': {'Pippin', 'Merry', 'Sam', 'Frodo'}, 'magician': {'Saruman', 'Gandalf'}, 'other': {}}
Prints -> {'hobbit': {'Pippin', 'Sam', 'Frodo', 'Mary'}, 'magician': {'Gandalf', 'Saruman'}}
Suppose to organise some of the elements in the set returned by the second exercise in two different sets:
set_hobbit
that refers to the setset({"Frodo", "Sam", "Pippin", "Merry"})
, andset_magician
defined asset({"Saruman", "Gandalf"})
. Create a dictionary containing two pairs: one that associates the set of hobbits with the key"hobbit"
, and the other that associates the set of magicians with the key"magician"
.