Python中的SymPy Permutation.next_lex()
Permutation.next_lex() : next_lex()是一个Sympy Python库函数,用于返回按词典顺序排列的下一个排列组合,如果自己是按词典顺序排列的最后一个排列组合,则返回None。
语法 : sympy.combinatorics.permutations.Permutation.next_lex()
返回:按词典顺序排列的下一个排列组合
代码 #1 : next_lex() 示例
# Python code explaining
# SymPy.Permutation.next_lex()
# importing SymPy libraries
from sympy.combinatorics.partitions import Partition
from sympy.combinatorics.permutations import Permutation
# Using from sympy.combinatorics.permutations.Permutation.next_lex() method
# creating Permutation
a = Permutation([[2, 0], [3, 1]])
b = Permutation([1, 3, 5, 4, 2, 0])
print ("Permutation a - next_lex form : ", a.next_lex())
print ("Permutation b - next_lex form : ", b.next_lex())
输出 :
Permutation a – next_lex form : (0 2 1 3)
Permutation b – next_lex form : (5)(0 1 4 3 2)
代码 #2 : next_lex() 例子 – 二维排布
# Python code explaining
# SymPy.Permutation.next_lex()
# importing SymPy libraries
from sympy.combinatorics.partitions import Partition
from sympy.combinatorics.permutations import Permutation
# Using from sympy.combinatorics.permutations.Permutation.next_lex() method
# creating Permutation
a = Permutation([[2, 4, 0],
[3, 1, 2],
[1, 5, 6]])
print ("Permutation a - next_lex form : ", a.next_lex())
输出 :
Permutation a – next_lex form : (6)(0 3 5)(1 2 4)