patternpythonMinor
Partitioning comments by date
Viewed 0 times
commentspartitioningdate
Problem
I have a list of
I would like to loop over
Comment objects comments. Each Comment has several properties, one of which is the date the comment was posted. I've sorted this list by date and there is at least 1 comment per date in a range.I would like to loop over
comments and for every date, create a new list of comments just for that day. However, the way I'm currently doing it feels very inefficient. Is there a more pythonic or efficient way to do this? (I've implemented a daterange(start_date, end_date) generator function according to this answer):comment_arrays = [] #array of arrays
for d in date_range(date(2015,1,1), date(2016,1,1)):
day_comments = []
for c in comments:
if c.date == d:
day_comments.append(c)
else if c.date > d:
comment_arrays.append(day_comments)Solution
I think this would a great situation to use
This of course groups all your comments, not only the ones in your date range. I'd use
itertools.groupby in.datekey = lambda x: x.date
comments.sort(key=datekey)
date_comments = [list(group) for date, group in itertools.groupby(comments, datekey)]This of course groups all your comments, not only the ones in your date range. I'd use
filter to skip the ones out of your range if you can't make use of the extra days comments.Code Snippets
datekey = lambda x: x.date
comments.sort(key=datekey)
date_comments = [list(group) for date, group in itertools.groupby(comments, datekey)]Context
StackExchange Code Review Q#120339, answer score: 3
Revisions (0)
No revisions yet.