RFeynman123
RFeynman123

Reputation: 31

Printing one list with respect to the other in Python

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]]

Upvotes: 1

Views: 58

Answers (4)

Jamiu S.
Jamiu S.

Reputation: 5723

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]]

Upvotes: 0

nickarafyllis
nickarafyllis

Reputation: 335

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

Upvotes: 0

tygzy
tygzy

Reputation: 716

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

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

Upvotes: 1

Talha Tayyab
Talha Tayyab

Reputation: 27257

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

Upvotes: 2

Related Questions