Itertools Groupby Questions

150 views Asked by At

I am struggling to understand how itertools.groupby works. I have an excel spreadsheet that has delivery dates in the first column followed by destination Lat in column 2 and destination Lon in column 3. Earlier I was able to get help with grouping the dates that were the same into subarrays of the larger array that holds them. Here is the code that does it.

with xlrd.open_workbook(file_location) as workbook:
    sheet = workbook.sheet_by_index(0)

    Dates = (sheet.cell_value(i,0) for i in range(sheet.nrows))
    Day = [list(group) for key, group in itertools.groupby(Dates)]

Now I need to take it a step further and group the Lat into one array and Lon into another array, but group them by the day. I've tried combining the the code listed above with something like something like this, but I do not know how to incorporate the Lat and the Lon variables into itertools groupby function.

with xlrd.open_workbook(file_location) as workbook:
    sheet = workbook.sheet_by_index(0)

    for i in range(sheet.nrows):
        Lat = (sheet.cell_value(i,2) for i in range(sheet.nrows))
        DeliveryX = [list(group) for key, group in itertools.groupby(Dates)]
        Lon = (sheet.cell_value(i,3) for i in range(sheet.nrows))
        DeliveryY = [list(group) for key, group in itertools.groupby(Dates)] 
1

There are 1 answers

0
Lynn On

groupby takes a key argument to decide how to group the values in the given list. You would essentially make an iterable of tuples like:

t = ((lat, lon, date), (lat, lon, date), ...)

You can achieve this by calling itertools.izip(Lat, Lon, Dates). Then, to chunk this list into groups by comparing the third fields, and extract the first and second ones, you can write

lats = [[i[0] for i in g] for k, g in groupby(t, key=lambda x: x[2])]
lats = [[i[1] for i in g] for k, g in groupby(t, key=lambda x: x[2])]