Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ SciPy คำนวณ Permutation และ Combination ใน Python

SciPy ใช้คำนวณ permutation และ combination ของสองค่าได้ ฟังก์ชัน 'perm' ในคลาส 'special' ของ SciPy

Syntax ของ 'perm'

scipy.special.perm(N, k)

วิธีใช้ SciPy คำนวณ Permutation และ Combination ใน Python

ตัวอย่าง Permutation

from scipy.special import perm
my_permute = perm(6, 2, exact=True)
print("The permutation of 6 and 2 is ")
print(my_permute)

ผลลัพธ์

The permutation of 6 and 2 is 30

คำอธิบาย

  • นำเข้าไลบรารี
  • ส่งพารามิเตอร์ให้ 'perm' คำนวณ
  • กำหนดตัวแปรและแสดงผล

Combination ด้วย SciPy

ฟังก์ชัน 'comb' ในคลาส 'special'

Syntax ของ 'comb'

scipy.special.comb(N, k)

วิธีใช้ SciPy คำนวณ Permutation และ Combination ใน Python

ตัวอย่าง Combination

from scipy.special import comb
my_comb = comb(6, 2, exact=True)
print("The combination of 6 and 2 is ")
print(my_comb)

ผลลัพธ์

The combination of 6 and 2 is 15

คำอธิบาย

  • นำเข้าไลบรารี
  • ส่งพารามิเตอร์ให้ 'comb' คำนวณ
  • กำหนดตัวแปรและแสดงผล

Permutation P(n,k) = n!/(n-k)! Combination C(n,k) = n!/(k!(n-k)!)