如何在循环时将项目附加到新的不同列表中?

2024-04-19 15:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Facebook图形API,希望从公共Facebook页面收集所有帖子和这些帖子的所有评论。你知道吗

我已经编写了一个代码来检索一篇文章的所有评论,但是现在我希望所有评论都包含在所有文章的列表中。不过,我不希望他们在同一个名单,但我希望每个职位不同的名单。。或者至少每个帖子的评论是分开的(我也在想一个有1个键(帖子)和多个值(评论)的dict)

我该怎么做?我为获得1篇帖子的评论而编写的代码是:

commentsperpost=[]
while True:
    i = 0
    try:
       commentsperpost+=[textpost(post=comment) for comment in comments3['data']]
       i += 1
       print(comments3['paging']['next'], i)       
       comments3 = requests.get(comments3['paging']['next']).json()
    except KeyError:
       break

一开始我想做一个循环,但是它把所有的评论都放到了同一个列表中。。你知道吗


Tags: 代码api图形列表facebook文章comment评论
1条回答
网友
1楼 · 发布于 2024-04-19 15:25:39

而不是:

commentsperpost+=[textpost(post=comment) for comment in comments3['data']]

你应该有:

commentsperpost.append([textpost(post=comment) for comment in comments3['data']])

相关问题 更多 >