[英]Retrieving a threaded comment list recursively
I am trying to write a recursive function that can retrieve the nested comments from a Reddit submission. 我正在尝试编写一个可以从Reddit提交中检索嵌套注释的递归函数。 I am using Python + PRAW
我正在使用Python + PRAW
def _get_comments(comments, ret = []):
for comment in comments:
if len(comment._replies) > 0:
return _get_comments(tail(comments), ret + [{
#"body": comment.body,
"id": comment.id,
"author": str(comment.author),
"replies": map(lambda replies: _get_comments(replies, []), [comment._replies])
}])
else:
return ret + [{
#"body": comment.body,
"id": comment.id,
"author": str(comment.author)
}]
return ret
def tail(list):
return list[1:len(list)]
And I get the following output, which is incomplete and has nested arrays: 我得到以下输出,它是不完整的,并且具有嵌套数组:
pprint(_get_comments(s.comments))
[{'author': 'wheremydirigiblesat',
'id': u'ctuzo4x',
'replies': [[{'author': 'rhascal',
'id': u'ctvd6jw',
'replies': [[{'author': 'xeltius', 'id': u'ctvx1vq'}]]}]]},
{'author': 'DemiDualism',
'id': u'ctv54qs',
'replies': [[{'author': 'rhascal',
'id': u'ctv5pm1',
'replies': [[{'author': 'blakeb43', 'id': u'ctvdb9c'}]]}]]},
{'author': 'Final7C', 'id': u'ctvao9j'}]
The Submission
object has a comments
attribute which is a list of Comment
objects. Submission
对象有一个comments
属性,该属性是Comment
对象的列表。 Each Comment
object has a _replies
attribute which is a list of more Comment
s. 每个
Comment
对象都有一个_replies
属性,该属性是更多Comment
的列表。
What am I missing? 我想念什么? I gave it my best shot -- recursion is hard.
我尽力了-递归很难。
You got it almost correctly. 您几乎完全正确。 The problem is that you're trying to make recursion as something complex, when it's simple.
问题是,当简单时,您试图将递归复杂化。 You don't need
tail()
function as well as map()
function inside, since you're already iterating through comments. 您不需要在内部使用
tail()
函数,也不需要map()
函数,因为您已经在注释中进行迭代了。
I renamed your function in examples, since it converts comments to dicts actually. 我在示例中重命名了您的函数,因为它实际上将注释转换为字典。
Let's start from simple case, think about it like: "okey, I want to have a function, which is able to convert list of comments to list of dicts". 让我们从简单的案例开始,像这样思考:“好了,我想要一个函数,该函数能够将注释列表转换为字典列表”。 Just simple function:
只是简单的功能:
def comments_to_dicts(comments):
results = [] # create list for results
for comment in comments: # iterate over comments
item = {
"id": comment.id,
"author": comment.author,
} # create dict from comment
results.append(item) # add converted item to results
return results # return all converted comments
And now you want dict to also include list of replies converted to dicts. 现在,您希望字典也包含转换为字典的回复列表。 And you already have function, which is able to do this conversion, so let's just use it and put result into
item['replies']
: 并且您已经有了可以执行此转换的函数,因此我们只需要使用它并将结果放入
item['replies']
:
def comments_to_dicts(comments):
results = [] # create list for results
for comment in comments: # iterate over comments
item = {
"id": comment.id,
"author": comment.author,
} # create dict from comment
if len(comment._replies) > 0:
item["replies"] = comments_to_dicts(comment._replies) # convert replies using the same function
results.append(item) # add converted item to results
return results # return all converted comments
Since you modified the same function you call, it will convert all replies, no matter how deep they are. 由于您修改了调用的相同函数,因此无论其深度如何,它都会转换所有答复。 Hope it's more clear how recursion works.
希望更清楚递归的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.