3

I have a dictionary that looks like this:

{'Week 1': {'Game1': {'Away': 'Team3', 'Home': 'Team2', 'Venue': 5},
            'Game2': {'Away': 'Team1', 'Home': 'Team4', 'Venue': 6}},
 'Week 2': {'Game1': {'Away': 'Team1', 'Home': 'Team2', 'Venue': 4},
            'Game2': {'Away': 'Team4', 'Home': 'Team3', 'Venue': 1}},
 'Week 3': {'Game1': {'Away': 'Team1', 'Home': 'Team3', 'Venue': 6},
            'Game2': {'Away': 'Team2', 'Home': 'Team4', 'Venue': 4}} 
...}

The dictionary contains fixture seperated by 'Week', 'Game' then 'Home', 'Away' and 'Venue'.

Say I was looking to search for all fixtures for 'Team 1' from the dictionary above and get an output of:

{'Week 1': {'Game 2': {'Away: ': 'a', 'Home: ': 'd', 'Rink ': 6}},
 'Week 2': {'Game1': {'Away: ': 'a', 'Home: ': 'b', 'Rink ': 4}},
 'Week 3': {'Game1': {'Away: ': 'a', 'Home: ': 'c', 'Rink ': 6}}}

What would be the best way to do this?

I've tried using for loops

for week, games in dict:
    for game, details in games:
        for k, v in details:
            if v == 'Team1'
               newDict.update({week: {game: details}})

But i get ValueError: too many values to unpack

Am I on the right lines, or totally off base? Any help would be greatly appreciated

MrRBM97
  • 169
  • 10
  • 3
    `for game, details in games.items()`, `for k, v in details.items()`. Btw, do not use `dict` as variable name, you're shadowing class. – Olvin Roght Aug 10 '20 at 09:44
  • Olvin has this correct - if you use `.items()` on your dictionary calls your code should work. Nicely written question by the way - clear, complete, and well written! – PirateNinjas Aug 10 '20 at 09:46
  • Thanks for your comment Olvin. I have tried you method but I get: ``` or week, games in schedule: ValueError: too many values to unpack (expected 2) ```. I have also taken your advise on not using ``` dict ``` as a variable name. – MrRBM97 Aug 10 '20 at 10:07
  • Thank you PirateNinja. I am most greatful – MrRBM97 Aug 10 '20 at 10:08
  • 1
    You'll need to do `for week, games in dict.items()` as well. Basically, if you iterate over a dictionary it iterates over the dictionary keys only. If you need the key-value pairs, you should use `items`, which gives you the key and value for each item in the dictionary. – PirateNinjas Aug 10 '20 at 10:11

3 Answers3

0

Dict Comprehension:

{match: {game: details} for match, games in a.items() for game, details in games.items() for k, v in details.items() if v == 'Team1'}   
mustafasencer
  • 723
  • 4
  • 12
0

You can also just do:

{
  week: {game: info}
  for week, games in weeks
  for game, info in games
  if team in (info['Away'], info['Home'])
}

Putting it all together:

>>> weels = {'Week 1': {'Game1': {'Away': 'Team3', 'Home': 'Team2', 'Venue': 5},
...             'Game2': {'Away': 'Team1', 'Home': 'Team4', 'Venue': 6}},
...  'Week 2': {'Game1': {'Away': 'Team1', 'Home': 'Team2', 'Venue': 4},
...             'Game2': {'Away': 'Team4', 'Home': 'Team3', 'Venue': 1}},
...  'Week 3': {'Game1': {'Away': 'Team1', 'Home': 'Team3', 'Venue': 6},
...             'Game2': {'Away': 'Team2', 'Home': 'Team4', 'Venue': 4}} }
>>> team = 'Team1'
>>> {week: {game: info}
...   for week, games in weeks
...   for game, info in games
...   if team in (info['Away'], info['Home'])}
{'Week 1': {'Game2': {'Away': 'Team1', 'Home': 'Team4', 'Venue': 6}}, 'Week 2': {'Game1': {'Away': 'Team1', 'Home': 'Team2', 'Venue': 4}}, 'Week 3': {'Game1': {'Away': 'Team1', 'Home': 'Team3', 'Venue': 6}}}
rudolfovic
  • 3,163
  • 2
  • 14
  • 38
  • this would work, but it's quite hard to read and reason about. The key point in the question is that the questioner is missing "items". Not a problem here as you use the keys directly. – PirateNinjas Aug 10 '20 at 09:56
  • It may look difficult to read and reason about in the terminal, I've extracted the comprehension itself out into a separate block. Mapping and filtering is actually exactly what it's there for. – rudolfovic Aug 10 '20 at 09:59
  • 1
    @rudolfovic, you're slowing down your code by accessing to values using keys, it will be better to iterate over keys and values using `dict.items()`. – Olvin Roght Aug 10 '20 at 10:01
0

You can use a NestedDict and avoid using nested for loops or comprehensions.

from ndicts import NestedDict

d = {'Week 1': {'Game1': {'Away': 'Team3', 'Home': 'Team2', 'Venue': 5},
                'Game2': {'Away': 'Team1', 'Home': 'Team4', 'Venue': 6}},
     'Week 2': {'Game1': {'Away': 'Team1', 'Home': 'Team2', 'Venue': 4},
                'Game2': {'Away': 'Team4', 'Home': 'Team3', 'Venue': 1}},
     'Week 3': {'Game1': {'Away': 'Team1', 'Home': 'Team3', 'Venue': 6},
                'Game2': {'Away': 'Team2', 'Home': 'Team4', 'Venue': 4}}}
nd = NestedDict()

nd_team1 = {}
for key, value in nd.items():
    if value == "Team1":
            team1_record = nd.extract[key[:-1]]
            nd_team1.update(team1_record)
>>> nd_team1.to_dict()
{'Week 1': {'Game2': {'Away': 'Team1', 'Home': 'Team4', 'Venue': 6}},
 'Week 2': {'Game1': {'Away': 'Team1', 'Home': 'Team2', 'Venue': 4}},
 'Week 3': {'Game1': {'Away': 'Team1', 'Home': 'Team3', 'Venue': 6}}}

To install ndicts pip install ndicts

edd313
  • 1,109
  • 7
  • 20