當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python SymPy Permutation.from_sequence()用法及代碼示例


Permutation.from_sequence():from_sequence()是一個sympy Python庫函數,它返回從“ i”的排序元素中獲取“ i”所需的排列。如果需要任何自定義字符串,也可以將“鍵”作為參數傳遞。

用法:
sympy.combinatorics.permutations.Permutation.from_sequence()

返回:
返回所需的排列
從“ i”的排序元素中獲取“ i”


代碼1:from_sequence()示例

# Python code explaining 
# SymPy.from_sequence() 
  
# importing SymPy libraries 
from sympy.combinatorics.partitions import Partition 
from sympy.combinatorics.permutations import Permutation 
  
# Using from  
# sympy.combinatorics.permutations.Permutation.from_sequence() method  
  
# creating vectors 
a = [1, 0, 0, 0] 
  
b = [6, 5, 4, 3, 0, 0 ] 
  
# inversion forms 
print ("vector a - from_sequence form : \n", Permutation.from_sequence(a)) 
print ("vector b - from_sequence form : \n", Permutation.from_sequence(b))

輸出:

vector a – from_sequence form :
Permutation([3, 0, 1, 2])

vector b – from_sequence form :
Permutation([5, 4, 3, 2, 0, 1])

代碼2:from_sequence()示例–使用字符串和鍵

# Python code explaining 
# SymPy.from_sequence() 
  
# importing SymPy libraries 
from sympy.combinatorics.partitions import Partition 
from sympy.combinatorics.permutations import Permutation 
  
# Using from sympy.combinatorics.permutations.Permutation.from_sequence() method  
  
# creating vector 
a = [2, 3, 1, 0] 
  
# inverted vector of a 
print ("vector a - from_sequence form : ",  
       Permutation.from_sequence(a)) 
  
# length = 5, so permutation as per that. 
print ("\nstring - from_sequence form : ",  
       Permutation.from_sequence('GEEKS')) 
  
  
# length = 5, so permutation as per that. 
# defining a key 
print ("\nstring - from_sequence form : ",  
       Permutation.from_sequence('GEEKS', key = lambda x: x.lower()))

輸出:

vector a – from_sequence form : Permutation([2, 3, 1, 0])

string – from_sequence form : Permutation([2, 0, 1, 3, 4])

string – from_sequence form : Permutation([2, 0, 1, 3, 4])



相關用法


注:本文由純淨天空篩選整理自noobestars101大神的英文原創作品 Python | SymPy Permutation.from_sequence() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。