Python-排列組合函數itertools.permutations


 

輸入兩個參數分別為:可迭代對象,從可迭代對象中取出幾個值來計算其排列

>>> from itertools import permutations
>>> permutations(a,3)
<itertools.permutations object at 0x000002216C0799F0>
>>> print(permutations(a,3))
<itertools.permutations object at 0x000002216E117270>
>>> type(permutations(a,3))
<class 'itertools.permutations'>
>>> 
>>> 
>>> 
>>> 
>>> for i in permutations('ABC',3):
    print(i)

    
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
>>> 
>>> for i in permutations(a,3):
    print(i)

    
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
>>> 

 


免責聲明!

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



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