當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sympy.nC()用法及代碼示例

借助於sympy.nC()方法,我們可以在SymPy中計算給定長度的組合數。

用法: nC(items, k) 

參數:
items –要在其上計算組合的整數,列表或字符串。
k –一個整數,指定組合的長度。


返回值:返回給定長度的組合數。

示例1:

# import sympy  
from sympy import * 
  
items = "abca"
k = 3
print("Value of k = {} and the items are = {}".format(k, items)) 
   
# Use sympy.nC() method  
combinations = nC(items, k)   
      
print("Combinations : {}".format(combinations))  

輸出:

Value of k = 3 and the items are = abca
Combinations : 3

示例2:

# import sympy  
from sympy import * 
  
items = [2, 1, 3, 5, 4] 
k = 3
print("Value of k = {} and the items are = {}".format(k, items)) 
   
# Use sympy.nC() method  
combinations = nC(items, k)   
      
print("Combinations : {}".format(combinations))  

輸出:

Value of k = 3 and the items are = [2, 1, 3, 5, 4]
Combinations : 10


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.nC() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。