Python Tabulate

时间:2014-03-11 09:26:51

标签: python python-2.7 python-3.x

如何使用tabulate包在python中合并多个表?

在python中连接两个表时,

append无效。这些表是使用python中的tabulate包实现的。

table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
table_3 = table_1.append(table_2)
print table_3

3 个答案:

答案 0 :(得分:3)

对于“连接两个表”,您可能需要list.extend,而不是追加。追加将第二个列表作为单个项目插入第一个列表。另请注意,extend将就地修改源列表并返回None

>>> table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
>>> table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
>>> table_3 = table_1.extend(table_2)
>>> print table_3
None

table_3现在为None,而table_1已延长:

>>> print table_1
[['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3], ['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3]]

答案 1 :(得分:1)

list.appendextend在列表中改变列表并返回None,因此您的table_3会在其中获得None。您可以使用+添加连接两个列表:

In [251]: table_1 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
     ...: table_2 = [["Value_1",1,2],["Value_2",2,4],["Value_3",2,3]]
     ...: table_3 = table_1 + table_2
     ...: print table_3
[['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3], ['Value_1', 1, 2], ['Value_2', 2, 4], ['Value_3', 2, 3]]

答案 2 :(得分:0)

您可以应用追加,因为值将采用列表格式..........

  

table_1 = [[" Value_1",1,2],[" Value_2",2,4],[" Value_3",2,3] ]      table_2 = [[" Value_1",1,2],[" Value_2",2,4],[" Value_3",2,3]]      table_1.extend(TABLE_2)      print table_1