将列表值分配给2D列表的最佳方法

时间:2019-03-10 01:31:44

标签: python list

将值列表分配给具有不同索引的2D列表的最佳方法是什么?

temp_list = [2, 1, 1, 5]

我想遍历2D列表:

list_to_assign =
          [[0, 0, 0, 0],
           [0, 0, 0, 0],
           [2, 0, 0, 1000],
           [0, 0, 0, 0]]

对于temp_list的每个值,我想将该值分配给2D列表中每个内部列表的零索引。

我想要的结果是

[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 1000],
[5, 0, 0, 0]]

2 个答案:

答案 0 :(得分:1)

IIUC,请尝试在下面使用此列表理解:

print([[x] + y[1:] for x,y in zip(temp_list,list_to_assign)])

输出:

[[2, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1000], [5, 0, 0, 0]]

答案 1 :(得分:0)

java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib/arm64-v8a/libpython.so" not found
        at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
        at java.lang.System.loadLibrary(System.java:1657)
        at com.example.hellolibs.MainActivity.<clinit>(MainActivity.java:36)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6698)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)