如何使用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
答案 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.append
或extend
在列表中改变列表并返回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