I have a list of objects and want to filter them according to some criteria. I can do it with list comprehension:
import datetime, pytz
# let's have a range of 100 hourly datetimes (just an example!):
dates = [ datetime.datetime(2010, 10, 1, 0, 0, 0, 0, pytz.utc) + datetime.timedelta(hours=i) for i in xrange(100) ]
# now I want a list of all dates, where hour in (0, 6, 12, 18)...
[ dt for dt in dates if dt.hour % 6 == 0 ]
Which works correctly.
How can I use the filter
function (for much larger datasets, therefore the speed is important)? I can check whether dt.hour is True (not 0):
import operator
filter(operator.attrrgetter('hour'), dates)
but how can I put also the (6).__rmod__
part to it, which will tell me whether the hour
attribute is divisible by 6?