展开和展平衣衫agged的嵌套列表 [英] Expand and flatten a ragged nested list

查看:96
本文介绍了展开和展平衣衫agged的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道平坦化嵌套列表的主题之前已经详细介绍过,但是我认为我的任务有些不同,并且找不到任何信息.

I know that the topic of flattening a nested list has been covered in great detail before, however I think my task is a bit different and I couldn't find any info.

我正在编写一个刮板,作为输出,我得到了一个嵌套列表.顶级列表元素应该成为电子表格形式的数据行.但是,由于嵌套列表通常具有不同的长度,因此我需要在展平列表之前先对其进行扩展.

I am writing a scraper, and as output I get a nested list. The top level list elements are supposed to become rows for data in spreadsheet form. However, since the nested lists are often of different lengths, I need to expand them before flattening the list.

这是一个例子.我有

   [ [ "id1", [["x", "y", "z"], [1, 2]],    ["a", "b", "c"]],
     [ "id2", [["x", "y", "z"], [1, 2, 3]], ["a", "b"]],
     [ "id3", [["x", "y"],      [1, 2, 3]], ["a", "b", "c", ""]] ]

我最终想要的输出是

   [[ "id1", "x", "y",  z, 1, 2, "", "a", "b", "c", ""],
    [ "id2", "x", "y",  z, 1, 2,  3, "a", "b",  "", ""],
    [ "id3", "x", "y", "", 1, 2,  3, "a", "b", "c", ""]]

但是,这样的中间列表

   [ [ "id1", [["x", "y", "z"], [1, 2, ""]], ["a", "b", "c", ""]],
     [ "id2", [["x", "y", "z"], [1, 2,  3]], ["a", "b",  "", ""]],
     [ "id3", [["x", "y",  ""], [1, 2,  3]], ["a", "b", "c", ""]] ]

然后我可以将其弄平也可以.

which I can then simply flatten would also be fine.

顶级列表元素(行)是在每次迭代中构建的,并附加到完整列表中.我想最后转换整个列表会更容易吗?

The top-level list elements (rows) are built in every iteration, and appended to the full list. I guess it is easier to transform the full list at the end?

嵌套元素的结构应该是相同的,但是在这一点上我还不能确定.如果结构看起来像这样,我想我有一个问题.

The structure in which elements are nested should be the same, however I cannot be certain of it at this point. I guess I have a problem if the structure where to look like this.

   [ [ "id1", [[x, y, z], [1, 2]],             ["a", "b", "c"]],
     [ "id2", [[x, y, z], [1, 2, 3]], ["bla"], ["a", "b"]],
     [ "id3", [[x, y],    [1, 2, 3]],          ["a", "b", "c", ""]] ]

应该变成

   [[ "id1", x, y,  z, 1, 2, "",    "", "a", "b", "c", ""],
    [ "id2", x, y,  z, 1, 2,  3, "bla", "a", "b",  "", ""],
    [ "id3", x, y, "", 1, 2,  3,    "", "a", "b", "c", ""]]

感谢您的任何评论,如果这很琐碎,请原谅,我是Python的新手.

Thanks for any comments, and please excuse if this is trivial, I am rather new to Python.

推荐答案

对于递归生成器和itertools中的izip_longest函数,我为相同结构"情况提供了一个简单的解决方案.这段代码是针对Python 2的,但是经过一些调整(注释中已注明),可以使其在Python 3上正常工作.

I've got a simple solution for the "same structure" case, using a recursive generator and the izip_longest function from itertools. This code is for Python 2, but with a few tweaks (noted in comments) it can be made to work on Python 3:

from itertools import izip_longest # in py3, this is renamed zip_longest

def flatten(nested_list):
    return zip(*_flattengen(nested_list)) # in py3, wrap this in list()

def _flattengen(iterable):
    for element in izip_longest(*iterable, fillvalue=""):
        if isinstance(element[0], list):
            for e in _flattengen(element):
                yield e
        else:
            yield element

在Python 3.3中,由于 PEP 380 ,它将变得更加简单,它将允许递归步骤for e in _flatengen(element): yield e变为yield from _flattengen(element).

In Python 3.3 it will become even simpler, thanks to PEP 380 which will allow the recursive step, for e in _flatengen(element): yield e, to become yield from _flattengen(element).

这篇关于展开和展平衣衫agged的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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