2016-12-15 75 views
2

我需要將3個列表合併到一個列表中,以便我可以順利地將它插入到sqlite表中。python:結合SQLite表的列表清單

list1= [[a1,b1,c1],[a2,b2,c2]] 
list2= [[d1,e1,f1],[d2,e2,f2]] 

輸出應該是這樣的:

combined_list = [[a1,b1,c1,d1,e1,f1],[a2,b2,c2,d2,e2,f2]] 

我試圖sumlist1 + list2但都沒有像這個輸出工作。

回答

2

你可以試試這個:

from operator import add 

a=[[1, 2, 3], [4, 5, 6]] 
b=[['a', 'b', 'c'], ['d', 'e', 'f']] 
print a + b 
print map(add, a, b) 

輸出:

[[1, 2, 3], [4, 5, 6], ['a', 'b', 'c'], ['d', 'e', 'f']] 
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f']] 

編輯: 要添加兩種以上的數組:

u=[[]]*lists[0].__len__() 
for x in lists: 
    u=map(add, u, x) 
+0

如果有多個列表合併?我必須將4個列表合併成一個。 –

+0

您可以多次重複。 from operator import add 'u = map(add,a,b); 35 = map(add,u,c); u = map(add,u,d);或者嘗試使其自動化:'u = [[]] * lists [0] .__ len __();對於列表中的x:u = map(add,u,x);' –

+0

完美工作!謝謝 –