I have a list of 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 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)
I have a list of 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)
I have a list of 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)
Looping over list of objects Partitioning comments by date
More pythonic / efficient way to loop Looping over list of objects?
I have a list of CommentComment
objects comments
. Each CommentComment
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)
More pythonic / efficient way to loop over list of objects?
I have a list of 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)
Looping over list of objects
I have a list of 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)