Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向元组列表添加2个列表_Python - Fatal编程技术网

Python 向元组列表添加2个列表

Python 向元组列表添加2个列表,python,Python,我有一张这样的清单 [(12,3,1),(12,3,5)] 和另外两份名单 [4,2] 及 我想把这些添加到第一个列表中 [(12,3,4,'A',1),(12,3,2,'B',5) 它们必须处于这个位置,因为我计划删除元组中最后一个值为1的元组 ts = [(12, 3, 1), (12, 3, 5)] l1 = [4, 2] l2 = ['A', 'B'] [t[:-1] + to_insert + t[-1:] for t, to_insert in zip(ts, zip(l1

我有一张这样的清单

[(12,3,1),(12,3,5)]
和另外两份名单

 [4,2]

我想把这些添加到第一个列表中

[(12,3,4,'A',1),(12,3,2,'B',5)

它们必须处于这个位置,因为我计划删除元组中最后一个值为1的元组

ts = [(12, 3, 1), (12, 3, 5)]
l1 = [4, 2]
l2 = ['A', 'B'] 
[t[:-1] + to_insert + t[-1:] for t, to_insert in zip(ts, zip(l1, l2))]
>> [(12, 3, 4, 'A', 1), (12, 3, 2, 'B', 5)]

看,这里有一些魔力:

ts = [(12, 3, 1), (12, 3, 5)]
l1 = [4, 2]
l2 = ['A', 'B'] 
[t[:-1] + to_insert + t[-1:] for t, to_insert in zip(ts, zip(l1, l2))]
>> [(12, 3, 4, 'A', 1), (12, 3, 2, 'B', 5)]

你试过这个吗?看起来你正在把你的问题分成几个部分,然后把每一部分都贴出来,直到问题解决为止。我还没有看到您尝试使用它的代码。此外,您还必须解释为什么要将列表添加到它们所在的位置。位置是任意的吗?除了删除一个位置之外,还有什么其他原因保留这些位置吗?最好描述一下您试图编写的程序,而不是找出如何使您的解决方案工作。我有两个整数列表、一个字符列表和一个元组列表。我需要将它们连接在一起,然后根据它们在元组末尾是否有1删除其中的一些。您尝试过这样做吗?看起来你正在把你的问题分成几个部分,然后把每一部分都贴出来,直到问题解决为止。我还没有看到您尝试使用它的代码。此外,您还必须解释为什么要将列表添加到它们所在的位置。位置是任意的吗?除了删除一个位置之外,还有什么其他原因保留这些位置吗?最好描述一下您试图编写的程序,而不是找出如何使您的解决方案工作。我有两个整数列表、一个字符列表和一个元组列表。我需要将它们连接在一起,然后根据元组末尾是否有1删除其中的一些
def submerge(d, e, f):
    for g in d[:-1]:
        yield g
    yield e
    yield f
    yield d[-1] # if you want to remove the last element just remove this line

def merge(a, b, c):
    for d, e, f in zip(a, b, c):
        yield tuple(submerge(d, e, f))

a = [(12,3,1),(12,3,5)]
b = [4,2]
c = ['A','B'] 

print list(merge(a, b, c))