繁体   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