Skip to main content
 首页 » 编程设计

Perl 中的 Python itertools.product() 等价物

2025年05月04日14daizhj

在 Python 中,我可以使用 itertools.product(),文档中说它是“笛卡尔积,相当于嵌套的 for 循环”。

它在 Perl 中的等价物是什么?

Python 中的示例:

import itertools 
opts_list = [["A","B"], ["C","D","E"], ["F","G"]] 
print list(itertools.product(*opts_list)) 

给予:

[('A', 'C', 'F'), ('A', 'C', 'G'), ('A', 'D', 'F'), ('A', 'D', 'G'), ('A', 'E', 'F'), ('A', 'E', 'G'), ('B', 'C', 'F'), ('B', 'C', 'G'), ('B', 'D', 'F'), ('B', 'D', 'G'), ('B', 'E', 'F'), ('B', 'E', 'G')]

请您参考如下方法:

我最终使用了:

use Math::Cartesian::Product; 
cartesian {print "@_\n"} ["A","B"], ["C", "D", "E"], ["F", "G"];