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