Python排列组合

摘要:
product笛卡尔积  (有放回抽样排列)permutations排列  (不放回抽样排列)combinations组合,没有重复  (不放回抽样组合)combinations_with_replacement组合,有重复  (有放回抽样组合)˃˃˃importitertools˃˃˃foriinitertools.product('ABCD',repeat=2):...print(i)...(

product 笛卡尔积  (有放回抽样排列)

permutations 排列  (不放回抽样排列)

combinations 组合,没有重复  (不放回抽样组合)

combinations_with_replacement 组合,有重复  (有放回抽样组合)

>>> importitertools
>>> 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')>>> importitertools
>>> 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')

combinations和permutations返回的是对象地址,原因是在python3里面,返回值已经不再是list,而是iterators(迭代器), 所以想要使用,只用将iterator 转换成list 即可

免责声明:文章转载自《Python排列组合》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux下c++动态库的生成及使用DOS 如何取当前时间做为文件名?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python 练习题

python 练习题 day1 1.简述变量命名规范 7条: 1.由字母,数字和下划线组成 2.name = input(“>>>”) name变量是什么数据类型通过代码检测 type 3.if条件语句的基本结构? if 条件 : print() 4.用print打印出下面内容: ⽂能提笔安天下,武能上⻢定乾坤.⼼存谋略何⼈胜,...

python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940

1.os.path.abspath(path)  返回path规范化的绝对路径。    >>> os.path.abspath('test.csv')  'C:\Python25\test.csv'    >>> os.path.abspath('c:\test.csv')  'c:\test.csv'    &g...

centos7安装python 与卸载python

安装python 下载Python安装包 1 cd /usr/local/src 编译时要提前装好gcc编译器和zlib zlib-devel 1、下载文件 1 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 2、解压 1 tar -zxvf Python-3.6.0...

Python的路径引用

1、以HOME目录为准,进行跳转 sys.path.append(os.path.dirname(__file__) + os.sep + '../') from config import swordfishconf from utils import log from utils.mysql_base import MySQLBase 将程序的HO...

NLP常用Python开发工具

一、Numpy NumPy系统是Python的一种开源的数值计算包。 包括: 1、一个强大的N维数组对象Array; 2、比较成熟的(广播)函数 库; 3、用于整合C/C++和Fortran代码的工具包; 4、实用的线性代数、傅里叶变换和随机数生成函数。 numpy和稀疏矩阵运算包scipy配合使用更加方便。 安装: pip install numpy 二...

python 抓取cisco交换机配置文件

#!/usr/bin/python import sys import time import os import pexpect now = time.strftime('%Y-%m-%d',time.localtime(time.time()))aa = open ('/home/hanlei/test/%s/log.txt' % now, "w")...