首頁 >後端開發 >Python教學 >Python中排列組合計算操作的實作範例

Python中排列組合計算操作的實作範例

黄舟
黄舟原創
2017-10-14 10:24:213649瀏覽

這篇文章主要介紹了Python實現的排列組合計算操作,涉及Python數學運算的相關函數與使用技巧,需要的朋友可以參考下

本文實例講述了Python實現的排列組合計算操作。分享給大家供大家參考,如下:

1. 呼叫scipy 計算排列組合的具體數值


##

>> from scipy.special import comb, perm
>> perm(3, 2)
6.0
>> comb(3, 2)
3.0

2. 呼叫itertools 取得排列組合的全部情況數


>> from itertools import combinations, permutations
>> permutations([1, 2, 3], 2)
<itertools.permutations at 0x7febfd880fc0>
        # 可迭代对象
>> list(permutations([1, 2, 3], 2))
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>> list(combinations([1, 2, 3], 2))
[(1, 2), (1, 3), (2, 3)]

以上是Python中排列組合計算操作的實作範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn