码迷,mamicode.com
首页 > 编程语言 > 详细

几个有用的python函数 (笛卡尔积, 排列, 组合)

时间:2020-04-22 13:19:26      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:replace   tools   iter   with   有用   笛卡尔   nat   bin   perm   

product 笛卡尔积

permutations 排列

combinations 组合,没有重复

combinations_with_replacement 组合,有重复

>>> import itertools
>>> for i in itertools.product(ABCD, repeat = 2):
...     print i,
... 
(A, A) (A, B) (A, C) (A, D) (B, A) (B, B) (B, C) (B, D) (C, A) (C, B) (C, C) (C, D) (D, A) (D, B) (D, C) (D, D)
>>> for i in itertools.permutations(ABCD, 2):
...     print i,
... 
(A, B) (A, C) (A, D) (B, A) (B, C) (B, D) (C, A) (C, B) (C, D) (D, A) (D, B) (D, C)
>>> for i in itertools.combinations(ABCD, 2):
...     print i,
... 
(A, B) (A, C) (A, D) (B, C) (B, D) (C, D)
>>> for i in itertools.combinations_with_replacement(ABCD, 2):
...     print i,
... 
(A, A) (A, B) (A, C) (A, D) (B, B) (B, C) (B, D) (C, C) (C, D) (D, D)

转载自:http://www.tuicool.com/articles/vIRryi

几个有用的python函数 (笛卡尔积, 排列, 组合)

标签:replace   tools   iter   with   有用   笛卡尔   nat   bin   perm   

原文地址:https://www.cnblogs.com/cassielcode/p/12751293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!