用于简化组合构造(如排列、组合和笛卡尔积)的递归生成器称为组合迭代器。
在Python中,有4个组合迭代器:
- # 从 itertools 模块导入产品功能
- from itertools import product
-
- print("The cartesian product using repeat:")
- print(list(product([1, 2], repeat = 2)))
- print()
-
- print("The cartesian product of the containers:")
- print(list(product(['geeks', 'for', 'geeks'], '2')))
- print()
-
- print("The cartesian product of the containers:")
- print(list(product('AB', [3, 4])))
输出:
- The cartesian product using repeat:
- [(1, 1), (1, 2), (2, 1), (2, 2)]
-
- The cartesian product of the containers:
- [('geeks', '2'), ('for', '2'), ('geeks', '2