0

このようなリストがあります

[(12,3,1),(12,3,5)]

および他の 2 つのリスト

 [4,2]

['A','B'] 

これらを最初に作成するリストに追加したい

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

タプルの最後の値として 1 のものを削除する予定なので、それらはこの位置にある必要があります

4

2 に答える 2

3

ほら、ここにいくつかの魔法があります:

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)]
于 2013-01-26T15:14:29.947 に答える
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))
于 2013-01-26T15:20:04.617 に答える