[Python] Combining Lists According to Index

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    With the following lists:

    A=['asd','qwe','wer']
    B=[1,2,3]
    C=['ttt', 'ttt', 'ttt']
    D=[1,2,5]
    E=[0,30,0]


    The desired output is:

    F=['asd',1,'ttt',1,0,'qwe',2,'ttt',2,30,'wer',3,'ttt',5,0]


    My attempted solution:

    F=[]
    for i in E:
    F.append(A[E.index(i)])
    F.append(B[E.index(i)])
    F.append(C[E.index(i)])
    F.append(D[E.index(i)])
    F.append(E[E.index(i)])
    print(F)


    However the output here is:

    ['asd', 1, 'ttt', 1, 0, 'qwe', 2, 'ttt', 2, 30, 'asd', 1, 'ttt', 1, 0]


    You can see that 'asd' repeats and I'm not sure why.

    Continue reading...

Compartilhe esta Página