Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归检索线程注释列表_Python_Recursion_Functional Programming_Reddit_Praw - Fatal编程技术网

Python 递归检索线程注释列表

Python 递归检索线程注释列表,python,recursion,functional-programming,reddit,praw,Python,Recursion,Functional Programming,Reddit,Praw,我正在尝试编写一个递归函数,可以从Reddit提交中检索嵌套注释。我正在使用Python+PRAW def _get_comments(comments, ret = []): for comment in comments: if len(comment._replies) > 0: return _get_comments(tail(comments), ret + [{ #"body": comment.

我正在尝试编写一个递归函数,可以从Reddit提交中检索嵌套注释。我正在使用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)]
我得到以下输出,它是不完整的,具有嵌套数组:

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'}]
Submission
对象具有一个
comments
属性,该属性是
Comment
对象的列表。每个
Comment
对象都有一个
\u repress
属性,该属性是更多
Comment
的列表


我错过了什么?我尽了最大努力——递归很难实现。

您几乎正确地理解了它。问题是,当递归很简单时,您试图将其作为复杂的东西。您不需要
tail()
函数以及内部的
map()
函数,因为您已经在迭代注释了

我在示例中重命名了您的函数,因为它实际上将注释转换为dict

让我们从简单的例子开始,像这样思考:“好的,我想要一个函数,它能够将注释列表转换为dict列表”。只是简单的功能:

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
现在您希望dict还包括转换为dict的回复列表。您已经有了这个函数,可以进行这种转换,所以让我们使用它并将结果放入
项['replays']

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

由于您修改了调用的同一个函数,因此它将转换所有回复,无论回复有多深。希望能更清楚地了解递归的工作原理。

非常感谢。这是非常清楚的,相比之下,我是多么复杂,使它!