用于展平嵌套列表的递归生成器 [英] Recursive generator for flattening nested lists

查看:51
本文介绍了用于展平嵌套列表的递归生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名编程新手,在理解我的 Python 教科书(Magnus Lie Hetland 的Beginning Python")中的示例时遇到了一些困难.该示例适用于设计用于展平嵌套列表元素(具有任意深度)的递归生成器:

def 展平(嵌套):尝试:对于嵌套的子列表:对于展平(子列表)中的元素:屈服要素除了类型错误:产量嵌套

然后,您将按如下方式输入嵌套列表:

<预><代码>>>>列表(展平([[[1],2],3,4,[5,[6,7]],8]))[1,2,3,4,5,6,7,8]

我了解 flatten() 中的递归如何帮助缩减到此列表的最内层元素 '1',但我不明白当 '1' 实际传递回 flatten() 时会发生什么作为嵌套".我认为这会导致 TypeError(无法迭代数字),并且异常处理实际上会为生成输出做繁重的工作......但是使用 flatten() 的修改版本进行测试已经说服了我事实并非如此.相反,似乎 'yield element' 行是负责的.

也就是说,我的问题是……屈服元素"如何实际执行?似乎嵌套"将是一个列表 - 在这种情况下添加另一层递归 - 或者它是一个数字并且你会得到一个类型错误.

对此的任何帮助将不胜感激......特别是,我很想了解事件链,因为 flatten() 处理一个简单的例子,如:

list(flatten([[1,2],3]))

解决方案

我在函数中添加了一些检测:

def flatten(嵌套,深度=0):尝试:print("{}迭代 {}".format(' '*depth,nested))对于嵌套的子列表:对于 flatten(sublist, depth+1) 中的元素:print("{}得到了 {}".format(' '*depth, element))屈服要素除了类型错误:print('{}不可迭代 - return {}'.format(' '*depth,nested))产量嵌套

现在打电话

list(flatten([[1,2],3]))

显示

迭代 [[1, 2], 3]迭代 [1, 2]迭代 1不可迭代 - 返回 1回来了 1回来了 1迭代 2不可迭代 - 返回 2回来了 2回来了 2迭代 3不可迭代 - 返回 3回来了 3

I'm a programming newbie and am having some trouble understanding an example from my python textbook ("Beginning Python" by Magnus Lie Hetland). The example is for a recursive generator designed to flatten the elements of nested lists (with arbitrary depth):

def flatten(nested):
    try:
        for sublist in nested:
            for element in flatten(sublist):
                yield element
    except TypeError:
        yield nested

You would then feed in a nested list as follows:

>>> list(flatten([[[1],2],3,4,[5,[6,7]],8]))
[1,2,3,4,5,6,7,8]

I understand how the recursion within flatten() helps to whittle down to the innermost element of this list, '1', but what I don't understand is what happens when '1' is actually passed back into flatten() as 'nested'. I thought that this would lead to a TypeError (can't iterate over a number), and that the exception handling was what would actually do the heavy lifting for generating output... but testing with modified versions of flatten() has convinced me that this isn't the case. Instead, it seems like the 'yield element' line is responsible.

That said, my question is this... how can 'yield element' ever actually be executed? It seems like 'nested' will either be a list - in which case another layer of recursion is added - or it's a number and you get a TypeError.

Any help with this would be much appreciated... in particular, I'd love to be walked through the chain of events as flatten() handles a simple example like:

list(flatten([[1,2],3]))

解决方案

I have added some instrumentation to the function:

def flatten(nested, depth=0):
    try:
        print("{}Iterate on {}".format('  '*depth, nested))
        for sublist in nested:
            for element in flatten(sublist, depth+1):
                print("{}got back {}".format('  '*depth, element))
                yield element
    except TypeError:
        print('{}not iterable - return {}'.format('  '*depth, nested))
        yield nested

Now calling

list(flatten([[1,2],3]))

displays

Iterate on [[1, 2], 3]
  Iterate on [1, 2]
    Iterate on 1
    not iterable - return 1
  got back 1
got back 1
    Iterate on 2
    not iterable - return 2
  got back 2
got back 2
  Iterate on 3
  not iterable - return 3
got back 3

这篇关于用于展平嵌套列表的递归生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆