Last active
February 8, 2019 08:10
-
-
Save iakovgan/5041e0892feb2f69ef8b3b3c4fcd28d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" a small example for finding intervals | |
""" | |
from pprint import pprint | |
# Data are sorted already by user and date | |
data = [ | |
[ 'user1', '2016/1', 1, 15 ], | |
[ 'user1', '2016/2', 1, 15 ], | |
[ 'user1', '2016/3', 1, 15 ], | |
[ 'user1', '2016/6', 1, 15 ], | |
[ 'user1', '2016/7', 1, 15 ], | |
[ 'user1', '2016/8', 1, 15 ], | |
[ 'user2', '2016/3', 1, 15 ], | |
[ 'user2', '2016/6', 1, 15 ], | |
[ 'user2', '2016/7', 1, 15 ], | |
] | |
periods = [] | |
current_period = None | |
for line in data: | |
user, date, status, amount = line | |
year, month = map (int, date.split('/')) | |
if not current_period: | |
current_period = {"start": month, "end": month, "user": user} | |
else: | |
if abs(month - current_period["end"]) <= 1\ | |
and current_period ["user"] == user: | |
# period continues | |
current_period ["end"] = month | |
else: | |
# new period continues | |
periods.append(current_period) | |
current_period = {"start": month, "end": month, "user": user} | |
periods.append(current_period) | |
pprint (data) | |
pprint (periods) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment