2014-03-26 17 views
0

元組嗨,請我怎樣才能讓三分的元組進行(2S和1)的元組的2所列出的創建的3S

list1 = [(2345,7465), (3254,9579)] 
list2 = [{'type': '62', 'length': 0.15, 'lanes': 1, 'modes': 'cwt'}, {'type': '62',  'length': 0.16, 'lanes': 1, 'modes': 'cwt'}] 

輸出應該是這樣的:

list3 = [(2345,7465,{'type': '62', 'length': 0.15, 'lanes': 1, 'modes': 'cwt'}), (3254,9579,{'type': '62', 'length': 0.16, 'lanes': 1, 'modes': 'cwt'})] 
+0

@Martijn對不起,這是一個錯字,其現予以更正 – Nobi

回答

6

使用zip()以從配對名單,併產生的元組:

list3 = [(l1[0], l1[1], l2) for l1, l2 in zip(list1, list2)] 

演示:

>>> list1 = [(2345,7465), (3254,9579)] 
>>> list2 = [{'type': '62', 'length': 0.15, 'lanes': 1, 'modes': 'cwt'}, {'type': '62',  'length': 0.16, 'lanes': 1, 'modes': 'cwt'}] 
>>> [(l1[0], l1[1], l2) for l1, l2 in zip(list1, list2)] 
[(2345, 7465, {'lanes': 1, 'length': 0.15, 'type': '62', 'modes': 'cwt'}), (3254, 9579, {'lanes': 1, 'length': 0.16, 'type': '62', 'modes': 'cwt'})] 
0

使用列表中理解上的指數基於:

[ list1[i1]+(list2[i1],) for i1 in range(len(list1))] 

輸出:

[(2345, 7465, {'lanes': 1, 'length': 0.15, 'type': '62', 'modes': 'cwt'}), (3254, 9579, {'lanes': 1, 'length': 0.16, 'type': '62', 'modes': 'cwt'})]