Python| SymPy Partition.sort_key() 方法
Partition.sort_key() :sort_key()是一个 sympy Python库函数,它返回可用于字符串的规范键。规范键的这种排序基于分区的大小和元素,其与等级的联系被打破。
Syntax : sympy.combinatorics.partitions.Partition.sort_key()
Return : Conoanical key which can be used for string
代码 #1:sort_key() 示例
# Python code explaining
# SymPy.sort_key()
# importing SymPy libraries
from sympy.utilities.iterables import default_sort_key
from sympy.combinatorics.partitions import Partition
from sympy.abc import x
# Using from sympy.combinatorics.partitions.Partition.sort_key() method
g = Partition([134, 322])
e = Partition(list(range(3)))
k = Partition([44, x])
s = Partition([3, 4])
lst = [s, k, g + 1, k, e]
lst.sort(key = default_sort_key); lst
输出 :
[{{3, 4}}, {{44, x}}, {{44, x}}, {{134}, {322}}, {{0, 1, 2}}]
代码 #2:sort_key() 示例
# Python code explaining
# SymPy.sort_key()
# importing SymPy libraries
from sympy.utilities.iterables import default_sort_key
from sympy.combinatorics.partitions import Partition
from sympy.abc import x, y
# Using from sympy.combinatorics.partitions.Partition.sort_key() method
k = Partition([44, x, y])
s = Partition([3, 55, 12, 4])
lst = [k, s]
lst.sort(key = default_sort_key); lst
输出 :
[{{44, x, y}}, {{3, 4, 12, 55}}]