arrays - Itertools Groupby Questions -
i struggling understand how itertools.groupby works. have excel spreadsheet has delivery dates in first column followed destination lat in column 2 , destination lon in column 3. earlier able grouping dates same subarrays of larger array holds them. here code it.
with xlrd.open_workbook(file_location) workbook: sheet = workbook.sheet_by_index(0) dates = (sheet.cell_value(i,0) in range(sheet.nrows)) day = [list(group) key, group in itertools.groupby(dates)]
now need take step further , group lat 1 array , lon array, group them day. i've tried combining the code listed above something this, not know how incorporate lat , lon variables itertools groupby function.
with xlrd.open_workbook(file_location) workbook: sheet = workbook.sheet_by_index(0) in range(sheet.nrows): lat = (sheet.cell_value(i,2) in range(sheet.nrows)) deliveryx = [list(group) key, group in itertools.groupby(dates)] lon = (sheet.cell_value(i,3) in range(sheet.nrows)) deliveryy = [list(group) key, group in itertools.groupby(dates)]
groupby
takes key
argument decide how group values in given list. make iterable of tuples like:
t = ((lat, lon, date), (lat, lon, date), ...)
you can achieve calling itertools.izip(lat, lon, dates)
. then, chunk list groups comparing third fields, , extract first , second ones, can write
lats = [[i[0] in g] k, g in groupby(t, key=lambda x: x[2])] lats = [[i[1] in g] k, g in groupby(t, key=lambda x: x[2])]
Comments
Post a Comment