[英]elements from first row in a matrix operated with one element taken from each row, iterating with all the elements of rows
Consider a 2D list in python. 考虑一下python中的2D列表。 Taking the first list of given 2D list, I want to do XOR operation of each element from the first list with one element each from subsequent lists so that I have n-1 elements operated with that one element. 以给定的2D列表的第一个列表为例,我想对第一个列表中的每个元素与后续列表中的每个元素进行XOR操作,这样我就可以对该元素进行n-1个元素的操作。
Example - list = [[0,1], [0,1,2,3], [0,1,2]] 示例-列表= [[0,1],[0,1,2,3],[0,1,2]]
combinations formed will be - 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 形成的组合将是-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
condition - The size of any of the list or sublists is not predefined 条件-任何列表或子列表的大小均未预定义
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)
More explanation in the python documentation . python文档中有更多解释 。 The *
gives each elements of your initial list ( for_the_product
in my code) as an argument to the product
function. *
给出了初始列表的每个元素(在我的代码中为for_the_product
),作为product
函数的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.