Python中的SymPy Permutation.inversions()
Permutation.inversions() : inversions()是一个sympy Python库函数,用于返回参数中permutation的反转值的数量。
反转向量包括这些元素,其值表示排列组合中<它并位于其右侧的元素的数量。
返回:参数中的排列组合的倒置值数量
代码 #1 : inversions() 示例
# Python code explaining
# SymPy.Permutation.inversions()
# importing SymPy libraries
from sympy.combinatorics.partitions import Partition
from sympy.combinatorics.permutations import Permutation
# Using from
# sympy.combinatorics.permutations.Permutation.inversions() method
# creating Permutation
a = Permutation([[2, 0], [3, 1]])
b = Permutation([1, 3, 5, 4, 2, 0])
print ("Permutation a - inversions form : ", a.inversions())
print ("Permutation b - inversions form : ", b.inversions())
输出 :
Permutation a – inversions form : 4
Permutation b – inversions form : 9
代码 #2 : inversions() 例子 – 二维互换
# Python code explaining
# SymPy.Permutation.inversions()
# importing SymPy libraries
from sympy.combinatorics.partitions import Partition
from sympy.combinatorics.permutations import Permutation
# Using from sympy.combinatorics.permutations.Permutation.inversions() method
# creating Permutation
a = Permutation([[2, 4, 0],
[3, 1, 2],
[1, 5, 6]])
print ("Permutation a - inversions form : ", a.inversions())
输出 :
Permutation a – inversions form : 10