python中itertools里的product和permutation,,python中ite
python中itertools里的product和permutation,,python中ite
python中itertools里的product和permutation平时经常碰到全排列或者在n个数组中每个数组选一个值组成的所有序列等等问题,可以用permutation和product解决,很方便,所以在此mark一下吧直接上代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081from itertools import *if __name__ == ‘__main__‘: for j in permutations([2,5,6]): print(j) ‘‘‘ (2, 5, 6) (2, 6, 5) (5, 2, 6) (5, 6, 2) (6, 2, 5) (6, 5, 2) ‘‘‘ list1 = [1, 2, 3] list2 = [4, 5, 6] list3 = [7, 8, 9] for i in product(list1,list2,list3): print(i) ‘‘‘ (1, 4, 7) (1, 4, 8) (1, 4, 9) (1, 5, 7) (1, 5, 8) (1, 5, 9) (1, 6, 7) (1, 6, 8) (1, 6, 9) (2, 4, 7) (2, 4, 8) (2, 4, 9) (2, 5, 7) (2, 5, 8) (2, 5, 9) (2, 6, 7) (2, 6, 8) (2, 6, 9) (3, 4, 7) (3, 4, 8) (3, 4, 9) (3, 5, 7) (3, 5, 8) (3, 5, 9) (3, 6, 7) (3, 6, 8) (3, 6, 9) ‘‘‘ #[list2]*3表示[list2,list2,list2] #最前面的*号表示将[list2,list2,list2]列表解析成独立的参数 #也就是相当于入参是(list2,list2,list2) for i in product(*[list2]*3): print(i) ‘‘‘ (4, 4, 4) (4, 4, 5) (4, 4, 6) (4, 5, 4) (4, 5, 5) (4, 5, 6) (4, 6, 4) (4, 6, 5) (4, 6, 6) (5, 4, 4) (5, 4, 5) (5, 4, 6) (5, 5, 4) (5, 5, 5) (5, 5, 6) (5, 6, 4) (5, 6, 5) (5, 6, 6) (6, 4, 4) (6, 4, 5) (6, 4, 6) (6, 5, 4) (6, 5, 5) (6, 5, 6) (6, 6, 4) (6, 6, 5) (6, 6, 6)
python中itertools里的product和permutation
相关内容
- 用python切掉图片的黑边,,手机里有很多G里的截
- pywin32 - Python 在Windows COM 编程模块,,pywin32直接包
- Python 绝对路径添加,相对路径和绝对路径举例,import
- Python中利用Tesseract软件来识别图片中的英文与中文,
- python数据结构之bitmap,python六种数据结构,#_*_coding
- python交换数据的两列,python处理excel数据, python交换
- python第二课笔记,python编程课,什么是BIF?BIF
- python脚本修改hosts文件,python写脚本入门, 用python
- Windows系统Python包的安装(持续更新...),手机一键装
- 关于python,你有什么想要知道的吗?,python写我爱你的代
评论关闭