A correction to the code at the end. The test of self.total_num_of_items 
should precede the pop(0)

[EMAIL PROTECTED] wrote:
>
>
> On 7/20/07, *Bob Gailer* <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
>
>     Take advantage of slicing:
>        def create_grid(self):
>            table = []
>            for i in range(0, len( self.total_num_of_items),
>     self.max_num_of_items_per_row):
>              table.append(tuple(self.total_num_of_items[i : i +
>     self.max_num_of_items_per_row ]))
>            return table
>
>
> simply amazing.  Thank you.
One of the many "shifts" one makes in adjusting to "new" language
features. I made such a shift in 1974 when I switched from FORTRAN and
PL/I to APL.
>
>     OK - to address your original question:
>
>         def create_grid(self):
>             table = []
>             while self.total_num_of_items:
>                 row = []
>                 count = 0
>                 while count < self.max_num_of_items_per_row and
>     self.total_num_of_items:
>                     row.append(self.total_num_of_items.pop(0))
>                     count += 1
>                 table.append(tuple(row))
>             return table
>
>
> At first I  regarded you with quiet awe
We gurus bask in attention.
> , but then the nitpick in me saw the two "while 
> self.total_num_of_item" statements, and I was less pleased.
Oh all right, that costs us one more statement. Independently we can get
rid of count:

    def create_grid(self):
        table = []
        while True:
            row = []
            while len(row) < self.max_num_of_items_per_row:
                if not self.total_num_of_items:
                    return table
                row.append(self.total_num_of_items.pop(0))
            table.append(tuple(row))


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to