实现以下目标的有效方法是什么?
自:
aList = [[1, 2, 3], [2, 3, 4], [3, 4, 5], ...]
bList = [['a'], ['b'], ['c'], ...]
到:
Out = [('a', 1, 2, 3), ('b', 2, 3, 4), ('c', 3, 4, 5), ....]
答案 0 :(得分:1)
列表理解应该做到。内置函数zip
在每个迭代步骤中返回每个列表中的一个元素,然后使用+
运算符连接两个元素,最后通过调用内置函数将生成的子列表转换为元组tuple
演示:
In [46]: aList = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
In [47]: bList = [['a'], ['b'], ['c']]
In [48]: Out = [tuple(b + a) for b, a in zip(bList, aList)]
In [49]: Out
Out[49]: [('a', 1, 2, 3), ('b', 2, 3, 4), ('c', 3, 4, 5)]