如何将列表插入特定元素?

时间:2019-03-02 07:37:36

标签: python

我有两个列表:

T1=[[['A'], ['C']], [['B'], ['C']]]
T2=[[['A'], ['B']], [['B'], ['A']]]

我想结合两者:

我尝试过:

for i in range(len(T2)):
      T1.insert(i,T2)

结果显示如下:

[[[['A'], ['B']], [['B'], ['A']]], [[['A'], ['B']], [['B'], ['A']]], [['A'], ['C']], [['B'], ['C']]]

但是我希望结果应该是:

[[[['A'], ['C']], [['A'], ['B']], [['B'], ['A']]], [[['B'], ['C']], [['A'], ['B']], [['B'], ['A']]]]

1 个答案:

答案 0 :(得分:1)

T1=[[['A'], ['C']], [['B'], ['C']]]
T2=[[['A'], ['B']], [['B'], ['A']]]

final = []
for i in T1:
    ans= []
    ans.append(i)
    for j in T2:
        ans.append(j)

    final.append(ans)
print final

输出将是:

[[[['A'], ['C']], [['A'], ['B']], [['B'], ['A']]], [[['B'], ['C']], [['A'], ['B']], [['B'], ['A']]]]

用户想要的是这样的

 [
        [
            [[’A’], [’C’]], [[’A’], [’B’]], [[’B’], [’A’]]
        ], 
        [
            [[’B’], [’C’]], [[’A’], [’B’]], [[’B’], [’A’]]
        ]
]

第一个列表T1中的第一个元素与另一个列表T2中的元素组合。