我有一個包含多個串列的串列,這些串列同時包含多個串列。為簡單起見,假設我有:
x = [
[[1, 0], [0, 0], [0 , 0], [1, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
]
讓我們考慮以下變數:
y = [[0, 0], [0, 2], [0 , 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1]]
有沒有更蟒蛇的方式來獲得:
res = [[1, 0], [0, 2], [0 , 0], [1, 0], [0, 1], [0, 0], [0, 1], [0, 0], [0, 1]]
以外:
for i in range(len(y)):
res.append([y[i][0] x[0][i][0], y[i][1] x[0][i][1]])
uj5u.com熱心網友回復:
您可以使用串列推導并在 1 行中執行 t:
x_list = x[0] # Not sure why x is inside a list but I'm going with it
result = [[sum(subitmes) for subitems in zip(*items)] for items in zip(x_list, y)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/407419.html
標籤:
上一篇:在kotlin中成對洗掉重復項
下一篇:如何在R中輸出一個串列
