이 튜토리얼에서는 Python 의 내장 함수를 사용하여 문자열의 순열을 찾을 것입니다. 순열이라고 함 . 방법 순열 itertools에 있습니다. 모듈.
문자열의 순열을 찾는 절차
- itertools 가져오기 모듈.
- 문자열을 초기화합니다.
- itertools.permutations 사용 문자열의 순열을 찾는 방법입니다.
- 세 번째 단계에서 메서드는 개체를 반환하고 목록으로 변환합니다.
- 목록에 문자열의 순열이 튜플로 포함되어 있습니다.
예시
프로그램을 봅시다.
## importing the module
import itertools
## initializing a string
string = "XYZ"
## itertools.permutations method
permutaion_list = list(itertools.permutations(string))
## printing the obj in list
print("-----------Permutations Of String In Tuples----------------")
print(permutaion_list)
## converting the tuples to string using 'join' method
print("-------------Permutations In String Format-----------------")
for tup in permutaion_list:
print("".join(tup)) 출력
위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.
-----------Permutations Of String In Tuples----------------
[('X', 'Y', 'Z'), ('X', 'Z', 'Y'), ('Y', 'X', 'Z'), ('Y', 'Z', 'X'), ('Z', 'X', 'Y'), ('Z', 'Y', 'X')]
-------------Permutations In String Format-----------------
XYZ
XZY
YXZ
YZX
ZXY
ZYX 프로그램에 대해 궁금한 점이 있으면 댓글 섹션에 언급해 주세요.