行显示不正确
Rows not showing properly
我正在制作一个 Table 对象,您可以在其中看到列和行。当我尝试更新 table 时,table 没有正确更新。这是为什么?
这是我当前的代码:
class Table:
def __init__(self, headers, rows):
self.headers, self.rows = list(headers), list(rows)
self.content = [self.headers,
[i for i in self.rows]]
def show(self):
print(str(list(self.headers)).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", ""))
for i in self.rows:
tp = str(i).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", "")
print(tp)
def add(self, headers=None, rows=None):
if headers:
self.headers.extend(headers)
if rows:
self.rows.extend(rows)
def update(self):
self.content = [self.headers,
[i for i in self.rows]]
t = Table(["a", "b"], [[1, 2, 3], [4, 5, 6]])
t.show()
print("-"*20)
t.add("c", [7, 8, 9])
t.update()
t.show()
有人可以帮我吗?
正如我在评论中所说,我不太确定预期的输出。但如果是:
a | b | c
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
然后您必须在对 add
方法的调用中添加额外的括号。据我所知,它期待一个列表列表作为它的第二个参数。所以:
t.add("c", [[7, 8, 9]])
完成任务。
我正在制作一个 Table 对象,您可以在其中看到列和行。当我尝试更新 table 时,table 没有正确更新。这是为什么?
这是我当前的代码:
class Table:
def __init__(self, headers, rows):
self.headers, self.rows = list(headers), list(rows)
self.content = [self.headers,
[i for i in self.rows]]
def show(self):
print(str(list(self.headers)).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", ""))
for i in self.rows:
tp = str(i).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", "")
print(tp)
def add(self, headers=None, rows=None):
if headers:
self.headers.extend(headers)
if rows:
self.rows.extend(rows)
def update(self):
self.content = [self.headers,
[i for i in self.rows]]
t = Table(["a", "b"], [[1, 2, 3], [4, 5, 6]])
t.show()
print("-"*20)
t.add("c", [7, 8, 9])
t.update()
t.show()
有人可以帮我吗?
正如我在评论中所说,我不太确定预期的输出。但如果是:
a | b | c
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
然后您必须在对 add
方法的调用中添加额外的括号。据我所知,它期待一个列表列表作为它的第二个参数。所以:
t.add("c", [[7, 8, 9]])
完成任务。