Reputation: 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]]
Upvotes: 1
Views: 58
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
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
Reputation: 27257
[X[y] for y in sum(B, [])]
#[353.856161, 282.754301, 134.119055, 63.4573886]
Upvotes: 2