Hướng dẫn string permutation code in python - mã hoán vị chuỗi trong python

Trong ví dụ này, bạn sẽ học cách tính toán tất cả các hoán vị của chuỗi.

Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Python sau:

  • Chuỗi Python
  • Python cho vòng lặp

Hoán vị là phương pháp chọn các phần tử từ một tập hợp theo những cách khác nhau.

Ví dụ: số cách mà các ký tự từ yup có thể được chọn là yup, ypu, ____10,

yup
ypu
uyp
upy
puy
pyu
None
1,
yup
ypu
uyp
upy
puy
pyu
None
2,
yup
ypu
uyp
upy
puy
pyu
None
3 và không chọn bất kỳ.

Chúng tôi sẽ thực hiện tương tự trong các ví dụ sau.


Ví dụ 1: Sử dụng đệ quy

def get_permutation(string, i=0):

    if i == len(string):   	 
        print("".join(string))

    for j in range(i, len(string)):

        words = [c for c in string]
   
        # swap
        words[i], words[j] = words[j], words[i]
   	 
        get_permutation(words, i + 1)

print(get_permutation('yup'))

Đầu ra

yup
ypu
uyp
upy
puy
pyu
None

Trong ví dụ này, đệ quy được sử dụng để tìm các hoán vị của chuỗi yup.

  • IF điều kiện in
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    5 được truyền dưới dạng đối số nếu nó bằng chiều dài của
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    6.
  • Trong mỗi lần lặp của vòng lặp, mỗi ký tự của yup được lưu trữ trong
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    8.
  • Các yếu tố của các từ được hoán đổi. Theo cách này, chúng tôi đạt được tất cả các kết hợp khác nhau của các nhân vật.
  • Quá trình này tiếp tục cho đến khi đạt được độ dài tối đa.

Ví dụ 2: Sử dụng itertools

from itertools import permutations

words = [''.join(p) for p in permutations('pro')]

print(words)

Đầu ra

['pro', 'por', 'rpo', 'rop', 'opr', 'orp']

Trong ví dụ này, đệ quy được sử dụng để tìm các hoán vị của chuỗi yup.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Một hoán vị, còn được gọi là số sắp xếp của người Viking, hoặc đơn đặt hàng, là một sự sắp xếp lại các yếu tố của một danh sách được đặt hàng thành một thư từ một-một với chính S. Một chuỗi độ dài n có n! hoán vị. Ví dụ:

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA

    Chúng tôi có giải pháp hiện tại cho vấn đề này, vui lòng giới thiệu các hoán vị của một chuỗi đã cho bằng liên kết STL. Chúng ta cũng có thể giải quyết vấn đề này trong Python bằng cách sử dụng các hoán vị chức năng sẵn có (có thể sử dụng được). & NBSP;

    Python3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    0
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    1
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    2
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    4
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    5
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    7

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    9
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    0
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    1
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    8
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    5
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    7
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    8
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    9

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    0
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    1
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    2

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    3
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    4
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    030
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    7

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    0
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    1

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    5
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    3

    Output:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    Hoán vị và kết hợp trong hoán vị Python của một chuỗi nhất định với các ký tự lặp lại ý tưởng là sử dụng từ điển để tránh in bản sao. & Nbsp;Permutations of a given string with repeating characters The idea is to use dictionary to avoid printing duplicates. 

    Python3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    0
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    1
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    2
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    4
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    5
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    7

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    9
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    0
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    1
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    3

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    8
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    5
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    7
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    8
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    9

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    0
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    1
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    2

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    3
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    4
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    030
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    7

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    0
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    1

    Hoán vị và kết hợp trong hoán vị Python của một chuỗi nhất định với các ký tự lặp lại ý tưởng là sử dụng từ điển để tránh in bản sao. & Nbsp;

    yup5yup6

    yup5

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    1yup9

    Output:

    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    2
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    5
    O(n!) where n is the size of the string.
    Auxiliary Space: O(n!) 


    Làm thế nào để bạn tạo ra một hoán vị của một chuỗi trong Python?

    Để tìm tất cả các hoán vị có thể của một chuỗi đã cho, bạn có thể sử dụng mô -đun ITERTOOLS có một phương thức hữu ích gọi là hoán vị (Itable [, R]). Phương pháp này trả về hoán vị chiều dài r liên tiếp của các phần tử trong các bộ dữ liệu có thể lặp lại.use the itertools module which has a useful method called permutations(iterable[, r]). This method return successive r length permutations of elements in the iterable as tuples.

    Làm thế nào để bạn tìm thấy giá trị hoán vị trong Python?

    Đầu tiên nhập gói ITERTOOLS để thực hiện phương thức hoán vị trong Python.Phương thức này lấy một danh sách làm đầu vào và trả về một danh sách đối tượng các bộ dữ liệu chứa tất cả các hoán vị trong một biểu mẫu danh sách.import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form.

    Phương pháp hoán vị trong Python là gì?

    Một hoán vị, còn được gọi là số sắp xếp của người Viking, hoặc đơn đặt hàng, là một sự sắp xếp lại các yếu tố của một danh sách được đặt hàng thành một thư từ một-một với chính S.Một chuỗi độ dài n có n!hoán vị.Ví dụ: Đầu vào: STR = 'ABC' Đầu ra: ABC ACB BAC BCA CABA.a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Examples: Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA.

    Làm thế nào để bạn hoán vị một danh sách trong Python?

    Ngày nay, chúng ta sẽ học cách có được các hoán vị có thể của một danh sách bằng cách sử dụng các phương thức khác nhau trong Python ...
    nhập itertools ..
    L = [2, 4, 6].
    r = 2 ..
    P = list (itertools. Permutations (L, R)).
    print(p).