python 排列組合之itertools


python 2.6 引入了itertools模塊,使得排列組合的實現非常簡單:

import itertools  

有序排列:e.g., 4個數內選2個排列:

>>> print list(itertools.permutations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

無序組合:e.g.,4個數內選2個:

>>> print list(itertools.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

 

原文轉載自http://blog.csdn.net/flying881114/article/details/8482159

另請原諒我的不求甚解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM