在课堂上创建字典

时间:2014-11-20 02:27:07

标签: python

首先,这可能听起来令人困惑,因为说实话,我并不真正理解我自己的问题而我真的很抱歉(这就是我提出这个问题的原因)。

所以基本上我必须根据给定的文档编写代码,但我并不真正理解我必须做的事情。请有人请帮帮我吗?

这是文档字符串:

class Table():
'''A class to represent a SQuEaL table'''
def set_dict(self, new_dict):
    '''(Table, dict of {str: list of str}) -> NoneType

    Populate this table with the data in new_dict.
    The input dictionary must be of the form:
        column_name: list_of_values
    '''
    pass 

def get_dict(self):
    '''(Table) -> dict of {str: list of str}

    Return the dictionary representation of this table. The dictionary keys
    will be the column names, and the list will contain the values
    for that column.
    '''

有没有人有任何想法?老实说,我花了几天时间才把它弄清楚,但我不能这样做。 请帮助我,并提前感谢你。

2 个答案:

答案 0 :(得分:0)

您似乎应该创建一个执行以下操作的类Table

实现get_dict方法,使Table.get_dict()返回一个字典,其键是列标题,其值是该列的行。

实现set_dict方法,Table.set_dict(some_dict)将使用您传递的字典填充表格,与上述规范相同。

如果它会有所帮助,想象一下表:

+--------------------------------------+
|Column1 | Column2 | Column3 | Column4 |
+--------------------------------------+
|C1R1    | C2R1    | C3R1    | C4R1    |
+--------------------------------------+
|C1R2    | C2R2    | C3R2    | C4R2    |
+--------------------------------------+

这将由表格的字典表示:

some_dict = {"Column1": ["C1R1", "C1R2"],
             "Column2": ["C2R1", "C2R2"],
             "Column3": ["C3R1", "C3R2"],
             "Column4": ["C4R1", "C4R2"]}

您的目标是编写一个类Table,以便:

table = Table()
table.set_dict(some_dict) # saves that dictionary
table.get_dict()          # produces that dictionary

答案 1 :(得分:0)

它会结束这样的事情。

class Table():
    def set_dict(self, new_dict):
        self.dict = new_dict

    def get_dict(self):
        return self.dict or {}
相关问题