Skip to content

Instantly share code, notes, and snippets.

@apalala
Created July 21, 2014 22:28
Show Gist options
  • Save apalala/494a84da76e543ab3bca to your computer and use it in GitHub Desktop.
Save apalala/494a84da76e543ab3bca to your computer and use it in GitHub Desktop.
Complex Lookp
# already sorted
for name, group in itertools.groupby(db_iter, key=lambda x: x[0]):
total_sales = 0
number_of_sales = 0
earliest_sale = None
latest_sale = None
for _, amount, date in group:
if amount is not None:
total_sales += amount
number_of_sales += 1
if date is not None:
if earliest_sale is None:
earliest_sale = date
latest_sale = date
employee_stats = dict(
total_sales=total_sales,
number_of_sales=number_of_sales,
earliest_sale=earliest_sale,
latest_sale=latest_sale,
)
report(name, employee_stats)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment