Using Groupby to Group a Column in an Access Table in Python
Question: I have been playing with the groupby function from the itertools module for a
while now (like days)

    
    
    for k, g in groupby(data, keyfunc):
    

I am really having trouble understanding the syntax. I've tried a variety of
different things, but I really don't know what to put in for "data" and
"keyfunc" and get it to return the groups of data I want in a print statement.

What am I doing is looping through rows in an access table.

I set a variable to a cursor search (which is sorted) in the access table and
pull out the column I want.

    
    
    for row in cursor:
        print row.ROAD_TYPE
    

This returns:

    
    
    TRUNK ROAD
    TRUNK ROAD
    TRUNK ROAD
    TRUNK ROAD
    COLLECTOR HIGHWAY
    COLLECTOR HIGHWAY
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    MAJOR ROAD
    MAJOR HIGHWAY
    

I want to group these values together and have it return the string value for
me so it prints something like this:

    
    
    TRUNK ROAD
    TRUNK ROAD
    TRUNK ROAD
    TRUNK ROAD
    
    COLLECTOR HIGHWAY
    COLLECTOR HIGHWAY
    
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    ACCESS ROAD
    
    MAJOR ROAD
    
    MAJOR HIGHWAY
    

Finally, I want to group a second column based on these new groups so that I
get something like this:

    
    
    TRUNK ROAD  M1
    TRUNK ROAD  M1
    
    TRUNK ROAD  M2
    
    TRUNK ROAD  M3
    
    
    COLLECTOR HIGHWAY  M1
    
    COLLECTOR HIGHWAY  M2
    
    
    ACCESS ROAD  M1
    ACCESS ROAD  M1
    
    ACCESS ROAD  M3
    ACCESS ROAD  M3
    
    ACCESS ROAD  M7
    ACCESS ROAD  M7
    
    ACCESS ROAD  M8
    
    
    MAJOR ROAD  M8
    
    
    MAJOR HIGHWAY  M8
    

I know this is probably way less difficult than I've made it out for myself, I
feel there's a simple answer, but I'm completely stumped and I can't seem to
find an example on the internet that explains the groupby syntax in a way that
I understand. Please feel f


Answer: 
    import itertools as it
    for key, group in it.groupby(cursor, lambda row: row.ROAD_TYPE):
        for sec_col,pairs in it.groupby(group, lambda row: row.SECOND_COLUMN):
            for row in pairs:
                print('{t}  {s}'.format(t=row.ROAD_TYPE,s=row.SECOND_COLUMN))
            print
        print
    

Here are two examples to help grok
[groupby](http://docs.python.org/library/itertools.html#itertools.groupby):

    
    
    [list(g) for k, g in it.groupby('AAAABBBCCD')]
    # [['A', 'A', 'A', 'A'], ['B', 'B', 'B'], ['C', 'C'], ['D']]
    

Above, all items which are the same, are grouped together.

Now we add a key function, `keyfunc`:

    
    
    keyfunc=lambda x: x//3
    data=range(13)
    [list(g) for k,g in it.groupby(data,keyfunc)]
    # [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12]]
    [k for k,g in it.groupby(data,keyfunc)]
    # [0, 1, 2, 3, 4]
    

Now instead of grouping by the items in data, we group according to
`keyfunc(x)` for each `x` in `data`.

