Python SymPy Partition.sort_key()方法
Partition.sort_key() : sort_key()是一个Sympy Python库函数,它返回可用于字符串的规范键。这个canonical key的排序是基于分区的大小和元素的联系被打破的等级。
语法 : sympy.combinatorics.partitions.Partition.sort_key()
返回:可用于字符串的对偶键
代码 #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}}]