gpt4 book ai didi

python - 根据两个日期时间之间的事件缩减列表

转载 作者:行者123 更新时间:2023-12-01 06:14:25 25 4
gpt4 key购买 nike

我有一个对象列表,其中包括日期,并且我需要创建该日期落在上个月任何时间(即上个月 1 号午夜)的所有对象的列表<目标数据<本月 1 日午夜。

我还需要满足此条件的对象总数。

现在,我正在一系列 while 循环中进行处理,但我觉得必须有更好的方法,特别是因为我的脚本挂起:

        post = 0 #the current post we're analyzing
posts = 0 #the total number of posts in the month we actually care about
lastmonthposts = [] #I think i can just get rid of this
blog = pyblog.WordPress()

date = blog.get_recent_posts(1 + posts)[0]['dateCreated']
while (date > startthismonth):
print "So far, there have been " + str(posts) + " posts this month we've counted."
post = post + 1
date = blog.get_recent_posts(1 + post)[0]['dateCreated']
while (date > startlastmonth):
print "So far, there have been " + str(posts) + " posts last month we've counted, which is " + str(date.timetuple().tm_mon) + "."
posts = posts + 1
post = post + 1
date = blog.get_recent_posts(1 + post)[0]['dateCreated']
lastmonthposts.append('blog')
for blogpost in lastmonthposts:
postnumber = blogpost['postid']
comments = comments + int(blog.get_comment_count(postnumber)['approved'])

最佳答案

我会使用 get_page_list() 而不是 get_recent_posts():

from datetime import datetime, timedelta

this_month_start = datetime.now().date().replace(day=1)
prev_month_start = (this_month_start - timedelta(days=1)).replace(day=1)

pages = blog.get_page_list()
last_month_pages = [
p for p in pages
if prev_month_start <= p['dateCreated'] < this_month_start]
last_month_approved_comment_count = sum(
blog.get_comment_count(page['page_id'])['approved']
for page in last_month_pages)

print "number of last month's pages:", len(last_month_pages)
print "number of approved comments for last month's pages:",
print last_month_approved_comment_count

关于python - 根据两个日期时间之间的事件缩减列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4072561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com