Python sympy.nP()方法
在sympy.nP()方法的帮助下,我们可以计算出SymPy中给定长度的排列组合的数量。
语法: nP(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.nP() method
permutations = nP(items, k)
print("Permutation : {}".format(permutations))
输出:
Value of k = 3 and the items are = abca
Permutation : 12
示例 #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.nP() method
permutations = nP(items, k)
print("Permutation : {}".format(permutations))
输出:
Value of k = 3 and the items are = [2, 1, 3, 5, 4]
Permutation : 60