簡體   English   中英

矩陣中第一行的元素,每行取一個元素,並與行的所有元素進行迭代

[英]elements from first row in a matrix operated with one element taken from each row, iterating with all the elements of rows

考慮一下python中的2D列表。 以給定的2D列表的第一個列表為例,我想對第一個列表中的每個元素與后續列表中的每個元素進行XOR操作,這樣我就可以對該元素進行n-1個元素的操作。

示例-列表= [[0,1],[0,1,2,3],[0,1,2]]

形成的組合將是-0 ^ 0 ^ 0、0 ^ 0 ^ 1、0 ^ 0 ^ 2、0 ^ 1 ^ 0、0 ^ 1 ^ 1、0 ^ 1 ^ 2、0 ^ 2 ^ 0、0 ^ 2 ^ 1、0 ^ 2 ^ 2、0 ^ 3 ^ 0、0 ^ 3 ^ 1、0 ^ 3 ^ 2、1 ^ 0 ^ 0、1 ^ 0 ^ 1、1 ^ 0 ^ 2、1 ^ 1 ^ 0 ,1 ^ 1 ^ 1、1 ^ 1 ^ 2、1 ^ 2 ^ 0、1 ^ 2 ^ 1、1 ^ 2 ^ 2、1 ^ 3 ^ 0、1 ^ 3 ^ 1、1 ^ 3 ^ 2

條件-任何列表或子列表的大小均未預定義

for_the_product = [[0,1], [0,1,2,3], [0,1,2]]
for one_tuple in itertools.product(*for_the_product):
    do_something_with(one_tuple)

python文檔中有更多解釋 *給出了初始列表的每個元素(在我的代碼中為for_the_product ),作為product函數的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM