comple list slices

William Meyer wmmeyer at gmail.com
Tue Feb 28 09:14:04 EST 2006


Hi,

I have a list of rows which contains a list of cells (from a html table), and I
want to create an array of logical row groups (ie group rows by the rowspan). I
am only concerned with checking the rowspan of specific columns, so that makes
it easier, but I am having trouble implementing it in python. In perl/c I could
use a for loop and modify the control variable as I walked the array:

my (@rowgroups);
for (my ($i) = 0; $i < $#rows; $i++) {
    my ($rowspan) = $rows[$i][0]->attr("rowspan") || 1;
    $rowspan--; # 0 indexed

    push @rowgroups, $rows[$i .. $i+$rowspan];

    $i += $rowspan;
}

but in python I can only come up with this:

rowgroups = []
rowspan = 0
for i in rows:
    if rowspan > 0:
        rowspan -= 1
        continue

    rowspan = rows[j][0]["rowspan"] or 1
    rowgroups.append(rows[ rows.index(i) : rows.index(i) + rowspan ])

    rowspan -= 1

I really dont like this solution. Any ideas?




More information about the Python-list mailing list