[Tutor] A couple beginner questions.

Jesse F. W jessefw@loop.com
Wed, 26 Sep 2001 19:08:17 -0700


Dear Eric,
	Below I have enclosed something which might serve for your needs.  The 
doc strings should make it clear how it works.
						Jesse W

Eric Henry wrote:
> Second, I need to store some information from a table.  Each 
> row has 3
> values.  Somewhat like this:
> 
> [1, 3, 4]
> [2, 4, 5]
> [3, 1, 1]

class Table:
    def __init__(self, table):
        """Create a table object.  Take a list of lists, which must include
        at least one row with the desired number of items in it.  Other rows can
        be added later.

            The provided list of rows is translated into a list of columns and
        saved to self.table."""
        self.table=[ [] for x in table[0] ]
        for item in table:
            for pos in range(len(table[0])):
                self.table[pos].append(item[pos])
    def add_row(self, row):
        """Add a row to the table.  Take a list of items.

            This checks if the row has the proper number of items, then appends
            each item in the row to the appropriete list in self.table"""
        if len(row) != len(self.table):
            return 'Error!'
        for pos in range(len(self.table)):
            self.table[pos].append(row[pos])
    def get(self, value, col_number):
        """Return a column, when given a value in that column, and the column
        number(starting from zero).

        If the given value is in the list representing the given column, return
        that list, else return None."""
        if value in self.table[col_number]:
            return self.table[col_number]
        return None