Nob*_*obi 0 python tuples list python-2.7
嗨,我怎么能从2个(2s和1s)元组列表中制作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'}]
Run Code Online (Sandbox Code Playgroud)
输出应如下所示:
list3 = [(2345,7465,{'type': '62', 'length': 0.15, 'lanes': 1, 'modes': 'cwt'}), (3254,9579,{'type': '62', 'length': 0.16, 'lanes': 1, 'modes': 'cwt'})]
Run Code Online (Sandbox Code Playgroud)
使用zip()从该配对名单,并产生的元组:
list3 = [(l1[0], l1[1], l2) for l1, l2 in zip(list1, list2)]
Run Code Online (Sandbox Code Playgroud)
演示:
>>> 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'})]
Run Code Online (Sandbox Code Playgroud)