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

โปรแกรม Python หา Permutation ของสตริงด้วย itertools (เวอร์ชัน 2)


บทความซ้ำกับ ID 1004106514 ใช้ itertools.permutations หา permutation ทั้งหมดของสตริง

โค้ดตัวอย่าง

from itertools import permutations

def allPermutations(str1):
    per = permutations(str1)
    print("Permutation Of this String:")
    for i in list(per):
        print(''.join(i))

if __name__ == "__main__":
    str1 = input("Enter the string: ")
    allPermutations(str1)

ผลลัพธ์

Enter the string: abc
Permutation Of this String:
abc
acb
bac
bca
cab
cba