[英]Printing one list with respect to the other in Python
I have two lists B
and X
.我有两个列表B
和X
I want to create a new list B1
which basically prints the values of X
with indices according to B
.我想创建一个新列表B1
,它基本上打印X
的值和根据B
的索引。 But instead of writing it manually, I want to do it at one step.但是我不想手动编写,而是想一步完成。 I present the expected output.我提出了预期的 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预期的 output 是
[[3.53856161e+02,2.82754301e+02],[1.34119055e+02,6.34573886e+01]]
[X[y] for y in sum(B, [])]
#[353.856161, 282.754301, 134.119055, 63.4573886]
for i in B:
for j in i:
j = X[j]
I haven't tested this but I believe it should work我没有测试过这个,但我相信它应该有效
B1 = [[X[i] for i in indices] for indices in B]
B1 = [["{:.8e}".format(X[i]) for i in m] for m in B]
Output: Output:
[[3.53856161e+02,2.82754301e+02],[1.34119055e+02,6.34573886e+01]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.