SymPy | Python中的 Permutation.inversions()
Permutation.inversions() : inversions()是一个 sympy Python库函数,它返回参数中排列的反转次数值。
反演向量包括其值指示编号的那些元素。排列中的元素 < it 并且位于其右侧。
Syntax : Return : number of inversions value of the permutation in argument
代码 #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() 示例– 2D 排列
# 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