Home > other >  Printing one list with respect to the other in Python
Printing one list with respect to the other in Python

Time:12-31

I have two lists B and X. I want to create a new list B1 which basically prints the values of X with indices according to B. But instead of writing it manually, I want to do it at one step. I present the expected output.

B=[[1,2],[3,4]]

X=[4.17551036e 02, 3.53856161e 02, 2.82754301e 02, 
            1.34119055e 02,6.34573886e 01, 2.08344718e 02, 1.00000000e-24]

B1=[[X[1],X[2]],[X[3],X[4]]]

The expected output is

[[3.53856161e 02,2.82754301e 02],[1.34119055e 02,6.34573886e 01]]

CodePudding user response:

[X[y] for y in sum(B, [])]
#[353.856161, 282.754301, 134.119055, 63.4573886]

CodePudding user response:

for i in B:
    for j in i:
        j = X[j]

I haven't tested this but I believe it should work

CodePudding user response:

B1 = [[X[i] for i in indices] for indices in B]

CodePudding user response:

B1 = [["{:.8e}".format(X[i]) for i in m] for m in B]

Output:

[[3.53856161e 02,2.82754301e 02],[1.34119055e 02,6.34573886e 01]]
  • Related