permutations
来源:互联网 发布:手机淘宝整点抢购技巧 编辑:程序博客网 时间:2024/06/16 11:33
列举数组中不同的排列方式,主要用于穷举法
from itertools import permutations#列表中所有不同排序方式的组合,此时allList为一个迭代器allList = permutations(['+','-','*','÷'])#可以只选择其中的三个allList = permutations(['+','-','*','÷'],3)
注意!不能进行多次循环,故不能直接放在for循环内
i = 0j = 0nums_possible = permutations((1,2,3))operators_possible = permutations(['+','-','*'])for nums in nums_possible: i += 1 for op in operators_possible: j += 1print i #6print j #6debug跟踪发现,当i=1时,operators_possible第一次循环是正常的,可是后面就不会再进入循环了
这是因为第一次就已经迭代完了,调用了next里面的
raise StopIteration()
具体可参考
http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/
i = 0j = 0nums_possible = permutations((1,2,3))operators_possible = list(permutations(['+','-','*']))for nums in nums_possible: i += 1 for op in operators_possible: j += 1print i #6print j #36
0 0
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Android事件分发机制
- Java-IO-字节流和字符流
- 得到JavaScript对象
- 【hdu 2063】过山车(匈牙利算法)
- 指针数组和数组指针
- permutations
- CheckBox 选中效果 选中子自动选中父
- Android开发:Handler Runnable和Thread之间的区别和联系 应用--------------------看完本篇,从此一览无余!
- HTTP中application/x-www-form-urlencoded字符说明
- 在学习 Thread 中 学到的定时器工具 Timer 和 TimerTask 理解
- 我的第一个oracle trigger 报错ora-04098:触发器无效且未通过重新验证
- 坚持#第91天~提高效率!
- [JZOJ4866] 禅与园林艺术
- 算法-冒泡排序