代码之家  ›  专栏  ›  技术社区  ›  Suncatcher

通过循环解包二维列表

  •  0
  • Suncatcher  · 技术社区  · 9 年前

    我需要打开具有动态长度的二维列表(列表列表)。整个任务是在 lxml

    this excellent answer 作为主干,我为我的任务编写了以下代码:

     page = (
        E.html(
            E.body(
            E.table(
                    E.tr(
                        E.th(E.div("header1")),
                          ...
                        E.th(E.div("header40")),
                        ),
                    *[E.tr(
                         *[
                            E.td(str(col)) for col in p_list[1] <<- how to put N here???
                         ]
                        ) for row in range(len(p_list))]
                    , border="2"
                    )
                )
            )
        )
    

    E.tr 是我的表的标题,第二个 *[E.tr p_list

    p_列表

    p_list = list()
    rows = table.iter('div')
    p_list.append([c.text for c in rows])
    rows = table.xpath("body/table")[0].findall("tr")
    for row in rows[2:]:
       p_list.append([c.text for c in row.getchildren()])
    

    相同的

    无法想出如何执行此操作。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Błotosmętek    9 年前

    代替 p_list[1] 具有 p_list[row] .更好的是,不要使用 range

    *[E.tr(
       *[ E.td(str(col)) for col in row ]
    ) for row in p_list ]