使用嵌套列表推导:
otherlist = [[(L[0], t) for t in L[1]] for L in lst]
因此对于 中的每个元素L
,lst
我们创建一个包含 的元组的新列表(L[0], elements of L[1])
。
演示:
>>> lst = [(1, [('a1', 'b1', 'c1'), ("a1'", "b1'", "c1'"), ('a1"', 'b1"', 'c1"')]), (2, [('a3', 'b3', 'c3'), ("a3'", "b3'", "c3'"), ('a3"', 'b3"', 'c3"')]), (3, [('a3', 'b3', 'c3'), ("a3'", "b3'", "c3'"), ('a3"', 'b3"', 'c3"')])]
>>> [[(L[0], t) for t in L[1]] for L in lst]
[[(1, ('a1', 'b1', 'c1')), (1, ("a1'", "b1'", "c1'")), (1, ('a1"', 'b1"', 'c1"'))], [(2, ('a3', 'b3', 'c3')), (2, ("a3'", "b3'", "c3'")), (2, ('a3"', 'b3"', 'c3"'))], [(3, ('a3', 'b3', 'c3')), (3, ("a3'", "b3'", "c3'")), (3, ('a3"', 'b3"', 'c3"'))]]