定义:
设A,B为集合,用A中元素为第一元素,B中元素为第二元素构成的有序对,所有这样的有序对组成的集合
__author__ = 'root'
import itertools
left = [[1, 1, 1], [1, 2, 2], [2, 2, 1]]
right = [[1, 2, 2], [1, 3, 2], [2, 2, 1]]
for x in itertools.product(left, right):
print x[0] + x[1]
运行结果:
/usr/bin/python2.7 /codes/python/PPro/Product.py
[1, 1, 1, 1, 2, 2]
[1, 1, 1, 1, 3, 2]
[1, 1, 1, 2, 2, 1]
[1, 2, 2, 1, 2, 2]
[1, 2, 2, 1, 3, 2]
[1, 2, 2, 2, 2, 1]
[2, 2, 1, 1, 2, 2]
[2, 2, 1, 1, 3, 2]
[2, 2, 1, 2, 2, 1]
Process finished with exit code 0
python对集合的操作还真是简洁~
package proj; import java.util.ArrayList; import java.util.List; public class Product { public static void main(String[] args) { List<Integer> left = new ArrayList<Integer>(); List<Integer> right = new ArrayList<Integer>(); left.add(1); left.add(2); left.add(3); right.add(4); right.add(5); right.add(6); for (Integer l : left) { for (Integer r : right) { System.out.println(l + " " + r); } } } } result: 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6