Python 计算笛卡尔积

import itertools


class cartesian(object):
    def __init__(self):
        self._data_list = []
        self._ans = []

    def add_data(self, data=[]):  #添加生成笛卡尔积的数据列表
        self._data_list.append(data)

    def build(self):  #计算笛卡尔积
        for item in itertools.product(*self._data_list):
            self._ans.append(item)
        return self._ans
 
if __name__=="__main__":
    car=cartesian()
    car.add_data([1,2,3,4])
    car.add_data([5,6,7,8])
    car.add_data([9,10,11,12])
    car.build()

'''
(1, 5, 9)
(1, 5, 10)
......
'''

 

0