Home > other >  Creating a dictionary from another dictionary where keys' = (values in previous dictionary) and
Creating a dictionary from another dictionary where keys' = (values in previous dictionary) and

Time:07-16

I have a dictionary set up as movie: {actors}. Below is a snippet of this dictionary

movie_dict = {
    'Sleepers': {'Brad Pitt', 'Kevin Bacon', 'Dustin Hoffman'}, 
    'Troy': {'Brad Pitt', 'Diane Kruger'}, 
    'Meet Joe Black': {'Brad Pitt', 'Anthony Hopkins'}, 
    'Oceans Eleven': {'Julia Roberts', 'Brad Pitt', 'George Clooney'}, 
    'Seven': {'Brad Pitt', 'Morgan Freeman'}
}

I am trying to create a dictionary of Co-Stars so that every actor is a key, and its values are a set of its costars.

Thanks in advance for any help! :)

CodePudding user response:

You could use collections.defaultdict:

>>> from collections import defaultdict
>>> movie_dict = {
...     'Sleepers': {'Brad Pitt', 'Kevin Bacon', 'Dustin Hoffman'},
...     'Troy': {'Brad Pitt', 'Diane Kruger'},
...     'Meet Joe Black': {'Brad Pitt', 'Anthony Hopkins'},
...     'Oceans Eleven': {'Julia Roberts', 'Brad Pitt', 'George Clooney'},
...     'Seven': {'Brad Pitt', 'Morgan Freeman'}
... }
>>> costars = defaultdict(set)
>>> for actors in movie_dict.values():
...     for actor in actors:
...         costars[actor] |= actors - {actor}
...
>>> for actor, actors_costars in costars.items():
...      print(f'{actor}: {actors_costars}')
...
Brad Pitt: {'Anthony Hopkins', 'George Clooney', 'Julia Roberts', 'Diane Kruger', 'Dustin Hoffman', 'Kevin Bacon', 'Morgan Freeman'}
Kevin Bacon: {'Brad Pitt', 'Dustin Hoffman'}
Dustin Hoffman: {'Brad Pitt', 'Kevin Bacon'}
Diane Kruger: {'Brad Pitt'}
Anthony Hopkins: {'Brad Pitt'}
George Clooney: {'Julia Roberts', 'Brad Pitt'}
Julia Roberts: {'George Clooney', 'Brad Pitt'}
Morgan Freeman: {'Brad Pitt'}

CodePudding user response:

Maybe this is what you are looking for.

output = {}
for movie, actors in movie_dict.items():
    for actor in actors:
        if actor in output:
            [output[actor].add(i) for i in actors if i != actor]
        else:
            output[actor] = set([i for i in actors if i != actor])
print(output)

output

{'Brad Pitt': {'Anthony Hopkins',
  'Diane Kruger',
  'Dustin Hoffman',
  'George Clooney',
  'Julia Roberts',
  'Kevin Bacon',
  'Morgan Freeman'},
 'Dustin Hoffman': {'Brad Pitt', 'Kevin Bacon'},
 'Kevin Bacon': {'Brad Pitt', 'Dustin Hoffman'},
 'Diane Kruger': {'Brad Pitt'},
 'Anthony Hopkins': {'Brad Pitt'},
 'Julia Roberts': {'Brad Pitt', 'George Clooney'},
 'George Clooney': {'Brad Pitt', 'Julia Roberts'},
 'Morgan Freeman': {'Brad Pitt'}}

CodePudding user response:

def type_key():
    Item_ = input("item key: ")
    return Item_

def Type_val():
    sett = set()
    x = 1
    while x:
        sett.add(input("value: "))
        x = 2
        while x not in (0, 1):
            x = int(input("0. break, 1. continue"))
    return sett

def craet_dic():
    dic = {}
    x = 1
    while x:
        dic[type_key()] = Type_val()
        x = 2
        while x nit in (0, 1):
            x = int(input("0. break, 1. continue: "
    return dic
print(craet_dic())
  • Related