展开并展平一个参差不齐的嵌套列表

时间:2012-08-16 09:33:57

标签: python nested-lists

我知道之前已经详细介绍了扁平化嵌套列表的主题,但我认为我的任务有点不同,我找不到任何信息。

我正在写一个刮刀,作为输出我得到一个嵌套列表。顶级列表元素应该成为电子表格形式的数据行。但是,由于嵌套列表通常具有不同的长度,因此我需要在展平列表之前展开它们。

这是一个例子。我有

   [ [ "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", ""]] ]

然后我可以简单地扁平化也可以。

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

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

   [ [ "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很陌生。

3 个答案:

答案 0 :(得分:6)

我使用递归生成器和izip_longest中的itertools函数为“相同结构”案例提供了一个简单的解决方案。这段代码适用于Python 2,但经过一些调整(在注释中注明),它可以在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),它将变得更加简单。

答案 1 :(得分:3)

实际上,对于结构不相同的通用案例,没有解决方案。 例如,普通算法会将["bla"]["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", "",  "",  ""]]

但是如果你知道你会有多个行,每个行都以ID开头,后面跟一个嵌套列表结构,下面的算法应该有效:

import itertools

def normalize(l):
    # just hack the first item to have only lists of lists or lists of items
    for sublist in l:
        sublist[0] = [sublist[0]]

    # break the nesting
    def flatten(l):
        for item in l:
            if not isinstance(item, list) or 0 == len([x for x in item if isinstance(x, list)]):
                yield item
            else:
                for subitem in flatten(item):
                    yield subitem

    l = [list(flatten(i)) for i in l]

    # extend all lists to greatest length
    list_lengths = { }
    for i in range(0, len(l[0])):
        for item in l:
            list_lengths[i] = max(len(item[i]), list_lengths.get(i, 0))

    for i in range(0, len(l[0])):
        for item in l:
            item[i] += [''] * (list_lengths[i] - len(item[i]))

    # flatten each row
    return [list(itertools.chain(*sublist)) for sublist in l]

l = [ [ "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", ""]] ]
l = normalize(l)
print l

答案 2 :(得分:0)

def recursive_pad(l, spacer=""):
    # Make the function never modify it's arguments.
    l = list(l)

    is_list = lambda x: isinstance(x, list)
    are_subelements_lists = map(is_list, l)
    if not any(are_subelements_lists):
        return l

    # Would catch [[], [], "42"]
    if not all(are_subelements_lists) and any(are_subelements_lists):
        raise Exception("Cannot mix lists and non-lists!")

    lengths = map(len, l)
    if max(lengths) == min(lengths):
        #We're already done
        return l
    # Pad it out
    map(lambda x: list_pad(x, spacer, max(lengths)), l)
    return l

def list_pad(l, spacer, pad_to):
    for i in range(len(l), pad_to):
        l.append(spacer)

if __name__ == "__main__":
    print(recursive_pad([[[[["x", "y", "z"], [1, 2]], ["a", "b", "c"]], [[[x, y, z], [1, 2, 3]], ["a", "b"]], [[["x", "y"], [1, 2, 3]], ["a", "b", "c", ""]] ]))

编辑:实际上,我误解了你的问题。这段代码解决了一个稍微不同的问题