借助於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
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python os.abort()用法及代碼示例
- Python os.WEXITSTATUS()用法及代碼示例
- Python os.fchmod()用法及代碼示例
- Python PIL getpixel()用法及代碼示例
- Python PIL putpixel()用法及代碼示例
- Python os.sysconf()用法及代碼示例
- Python os.confstr()用法及代碼示例
- Python os._exit()用法及代碼示例
- Python cmath.log()用法及代碼示例
- Python Tensorflow cos()用法及代碼示例
- Python sympy.rf()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.nC() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。