python几个不同list进行合并
a=[[1,2],[3,4]]b=[['a','b'],['c','d'],['e','f']]c=[['a1,0']]我想合并成list=[[1,2,'a','b','...
a = [[1,2],[3,4]]
b = [['a','b'],['c','d'],['e','f']]
c = [['a1,0']]
我想合并成
list = [ [1,2,'a','b','a1','0']
[3,4,'c','d','null','null']
[null','null','e','f','null','null']]
就是同一位置合并,没有的就写NULL
最多可能会有9个这样列表需要合并 展开
b = [['a','b'],['c','d'],['e','f']]
c = [['a1,0']]
我想合并成
list = [ [1,2,'a','b','a1','0']
[3,4,'c','d','null','null']
[null','null','e','f','null','null']]
就是同一位置合并,没有的就写NULL
最多可能会有9个这样列表需要合并 展开
2个回答
展开全部
a = [[1,2],[3,4]]
b = [['a','b'],['c','d'],['e','f']]
c = [['a1,0']]
data = [ a ,b ,c ]
count = max( len( x ) for x in data )
data = [ ( x + ( count - len( x ) ) * [ [ 'NULL' ,'NULL' ] ] ) for x in data ]
print( [ sum( x ,[ ] ) for x in zip( *data ) ] )
b = [['a','b'],['c','d'],['e','f']]
c = [['a1,0']]
data = [ a ,b ,c ]
count = max( len( x ) for x in data )
data = [ ( x + ( count - len( x ) ) * [ [ 'NULL' ,'NULL' ] ] ) for x in data ]
print( [ sum( x ,[ ] ) for x in zip( *data ) ] )
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/usr/bin/env python
# coding: utf-8
a = [[1,2],[3,4]]
b = [['a','b'],['c','d'],['e','f']]
c = [['a1', 0]]
d = [[10, 20],[11,21]]
additional = ['NULL', 'NULL']
def mergeList(*lsts):
maxlength = max(map(len, lsts))
data = map(lambda x: x + (maxlength - len(x)) * [additional], lsts)
return [sum(r, []) for r in map(list, zip(*data))]
import pprint
pprint.pprint( mergeList(a, b, c, d) )
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询