在python中附加列表(如果包含其他列表中的项)

在python中附加列表(如果包含其他列表中的项),python,Python,我有两份清单: list1 = [('a', '1'),('b', '2'),('c', '3') list2 = [('a', 'x'),('b', 'y'),('c', 'z') 我想创建: list3 = [('a', '1', 'x'),('b', '2', 'y'),('c', '3', 'z') 我尝试过。追加,但没有成功: list3 = list1.append(list2[1]) 您可以使用zip: list1 = [('a', '1'),('b', '2'),('c',

我有两份清单:

list1 = [('a', '1'),('b', '2'),('c', '3')
list2 = [('a', 'x'),('b', 'y'),('c', 'z')
我想创建:

list3 = [('a', '1', 'x'),('b', '2', 'y'),('c', '3', 'z')
我尝试过。追加,但没有成功:

list3 = list1.append(list2[1])

您可以使用
zip

list1 = [('a', '1'),('b', '2'),('c', '3')]
list2 = [('a', 'x'),('b', 'y'),('c', 'z')]
new_result = [(a, c, d) for (a, c), (_, d) in zip(list1, list2)]
输出:

[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[{'a', 'x', '1'}, {'y', '2', 'b'}, {'3', 'c', 'z'}]

用户
列表理解

list1 = [('a', '1'),('b', '2'),('c', '3')]
list2 = [('a', 'x'),('b', 'y'),('c', 'z')]
list3= [(list1[i][0],list1[i][1],list2[i][1]) for i in range(len(list1))]
print(list3)
输出:

[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[{'a', 'x', '1'}, {'y', '2', 'b'}, {'3', 'c', 'z'}]

您可以使用
联合

list1 = [('a', '1'),('b', '2'),('c', '3')]
list2 = [('a', 'x'),('b', 'y'),('c', 'z')]

list3 = []

for i in range(len(list1)):
  for j in range(len(list2)):
    if(i == j):
      list3.append(set(list1[i]) | set(list2[j]))
      break;
输出:

[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[('a', '1', 'x'), ('b', '2', 'y'), ('c', '3', 'z')]
[{'a', 'x', '1'}, {'y', '2', 'b'}, {'3', 'c', 'z'}]