comple list slices

William Meyer wmmeyer at gmail.com
Tue Feb 28 11:26:52 EST 2006


 <shandy.b <at> gmail.com> writes:

> 
> A couple questions:
> 
> 1- what is j?
> 2- what does the rows[x][y] object look like?  I assume it's a dict
> that has a "rowspan" key.  Can rows[x][y]["rowspan"] sometimes be 0?
> 
> Perhaps you're looking for something like this:
> rowgroups = []
> rowspan = 0
> for i in range( len(rows) ):
>     if rowspan <= 0:
>         rowspan = rows[j][0]["rowspan"]
>         if rowspan == 0:
>             rowspan = 1
> 
>         rowgroups.append(rows[ i : i + rowspan ])
> 
>     rowspan -= 1
> 
> -sjbrown
> 

oops, typo
row[j] should be i
that line should read:
    rowspan = i[0]["rowspan"]

rows is a list of lists which contains td tag objects from beautifulsoup.
rowspan is a tag attribute. You solution is clearer then mine, but the approach
is the same. Is there finer grained control of loops in python? (or in a perfect
world a way to group rows this way with list comprehensions?)

Rowspan can be none, meaning 1, and it can be zero. Zero means the row extends
from the current cell until the end of the table. That isnt used much, and not
in the table I am scraping at all.







More information about the Python-list mailing list