📜  SymPy | Python中的 Permutation.next_lex()(1)

📅  最后修改于: 2023-12-03 15:35:13.408000             🧑  作者: Mango

SymPy | Python中的 Permutation.next_lex()

在SymPy中,Permutation是一个可变的类,表示用于排列排列的对称群。Permutation的实例是一个排列,可以通过一系列排列操作来修改。其中之一是next_lex()方法,该方法返回字典顺序下的下一个排列。如果当前排列是字典顺序的最后一个排列,则返回None

示例
from sympy.combinatorics import Permutation

p = Permutation([1, 2, 3])
print(p)
# 输出 Permutation(1, 2, 3)

p = p.next_lex()
print(p)
# 输出 Permutation(1, 3, 2)

p = p.next_lex()
print(p)
# 输出 Permutation(2, 1, 3)

p = p.next_lex()
print(p)
# 输出 Permutation(2, 3, 1)

p = p.next_lex()
print(p)
# 输出 Permutation(3, 1, 2)

p = p.next_lex()
print(p)
# 输出 Permutation(3, 2, 1)

p = p.next_lex()
print(p)
# 输出 None
使用示例
from sympy.combinatorics import Permutation

p = Permutation([4, 2, 1, 3])

while p is not None:
    print(p)
    p = p.next_lex()

输出结果:

Permutation(1, 2, 3, 4)
Permutation(1, 2, 4, 3)
Permutation(1, 3, 2, 4)
Permutation(1, 3, 4, 2)
Permutation(1, 4, 2, 3)
Permutation(1, 4, 3, 2)
Permutation(2, 1, 3, 4)
Permutation(2, 1, 4, 3)
Permutation(2, 3, 1, 4)
Permutation(2, 3, 4, 1)
Permutation(2, 4, 1, 3)
Permutation(2, 4, 3, 1)
Permutation(3, 1, 2, 4)
Permutation(3, 1, 4, 2)
Permutation(3, 2, 1, 4)
Permutation(3, 2, 4, 1)
Permutation(3, 4, 1, 2)
Permutation(3, 4, 2, 1)
Permutation(4, 1, 2, 3)
Permutation(4, 1, 3, 2)
Permutation(4, 2, 1, 3)
Permutation(4, 2, 3, 1)
Permutation(4, 3, 1, 2)
Permutation(4, 3, 2, 1)
None
参数

next_lex()方法没有参数。

返回值

该方法返回字典顺序下的下一个排列。如果当前排列是字典顺序的最后一个排列,则返回None

总结

Permutation.next_lex()方法是一个方便的方法,可用于生成排列的下一个字典顺序。它非常容易使用,只需要使用Permutation类的实例调用该方法,即可返回下一个字典顺序。该方法非常有用,可用于生成排列的集合,以及在各种数学问题中使用排列。