Nested Dictionary Python with fantasy football data
Question: I'm trying to run my own statistics model to try and get an edge in my fantasy
football league, and I'm having troubles creating my data structure of
dictionaries. My data looks like this.

    
    
    Player  Team    G   Att Yard    TD  Target  Rec Yard    TD  FFPts   FFpts/G
    Adrian Peterson MIN 1   18  93  2   4   4   18  1   29.1    29.1
    Reggie Bush DET 1   21  90  0   8   4   101 1   25.1    25.1
    LeSean McCoy    PHI 1   31  184 1   1   1   5   0   24.9    24.9
    Joique Bell DET 1   6   25  2   6   5   67  0   21.2    21.2
    Jamaal Charles  KC  1   16  77  1   6   3   23  0   16.0    16.0
    Shane Vereen    NE  1   14  101 0   10  7   58  0   15.9    15.9
    

and I have files for each week of the season.

    
    
    {'Adrian Peterson': 
        {'week1.txt':
           {'Team':'MIN', 'G':'1','Att':'18'....}
        }
        {'week2.txt':
           {'Team':'MIN', 'G':'1','Att':'20'....}
        }
    }
    

However, when I run my code it overwrites each week, and I can't figure out
why.

Code:

    
    
    import os
    
    def makeDict():
        directory = os.listdir('.')
        print directory
        playerDict = {}
        for item in directory:
            if item != 'bayes.py' and item !='.DS_Store':
                f = open(item)
                headers = f.readline().split('\t')
                for line in f:
                        stats = line.replace('\n','').split('\t')
                        statDict = {}
                        weekDict = {}
                        for i,stat in enumerate(stats):
                            if i ==0:
                                player = stat
                            else:
                                statDict[headers[i]] = stat
                            if i == len(stats)-1:
                                weekDict[item] = statDict
                                playerDict[player] = weekDict
    
        return playerDict
    
    print makeDict()
    

It ends up that the dictionary only holds the information from the last file.
What am I doing wrong?


Answer: The reason is because the way you have structured your output dictionary is
wrong. If `Adrian Peterson` 's stats are going to be in several files, and you
wish to aggregate them under one key (i.e., his name) then
`playerDict[player_name]` should be a list.

So change your code to do the following:

  1. `from collections import defaultdict`
  2. `playerDict = defaultdict(list)`
  3. `playerDict[player].append(weekDict)` instead of `playerDict[player] = weekDict`

I duplicated `Adrian Peterson`'s data across different files, made the above
changes to your code and this is what I see:

    
    
    {
        'AdrianPeterson': [
            {
                'data': {
                    'FFPts': '29.1',
                    'Yard': '18',
                    'Target': '4',
                    'G': '1',
                    'Att': '188',
                    'Team': 'MIN',
                    'Rec': '4',
                    'TD': '1',
                    'FFpts/G\n': '29.1'
                }
            },
            {
                'data2': {
                    'FFPts': '29.1',
                    'Yard': '18',
                    'Target': '4',
                    'G': '1',
                    'Att': '188',
                    'Team': 'MIN',
                    'Rec': '4',
                    'TD': '1',
                    'FFpts/G\n': '29.1'
                }
            }
        ]
    }
    

Here `data` and `data2` were the name of my files. In your case, it would be
`week1.txt` and `week2.txt`

