Hướng dẫn how do you find the permutation of a string in python? - làm thế nào để bạn tìm thấy hoán vị của một 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,

yup
ypu
uyp
upy
puy
pyu
None
1,
yup
ypu
uyp
upy
puy
pyu
None
2,
yup
ypu
uyp
upy
puy
pyu
None
3,
yup
ypu
uyp
upy
puy
pyu
None
4,
yup
ypu
uyp
upy
puy
pyu
None
5 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
    7 đượ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
    8.
  • 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
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    0.
  • 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

    Đọc
    Method #1: Using Naive Method 
     

    Python3

    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)
    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

    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3 yup0
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    6 yup2
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8 yup4

    Xem thảo luận

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    0
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    1

    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    4
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    3
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    4

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    0
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    6
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    7
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    9
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    0

    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    1
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    223
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    4

    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    1
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    6
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    7
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    8
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    9

    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    1
    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    223
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    3

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    4
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    5
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    6
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    7
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    9
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    0

    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
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    3
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    5
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    6

    Output:

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']

    & nbsp; & nbsp; Phương thức #2: Sử dụng itertools & nbsp; & nbsp;
    Method #2: Using itertools 
     

    Python3

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    7
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    8
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    9
    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG
    0

    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

    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG
    8
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3 yup0
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    6 yup2
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8 yup4

    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)
    6yup7
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    8
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    00

    Output:

    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']


    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

    Đọc

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

    Bàn luận

    Python3

    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ụ:

    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;

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    7
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    8
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    9
    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG
    0

    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    2
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    06
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    55____108

    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    09
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    6
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    17
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    20

    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    21
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    23

    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    4
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    06
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    14

    Output:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    25
    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)
    3
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    28Permutations of a given string with repeating characters The idea is to use dictionary to avoid printing duplicates. 

    Python3

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    7
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    8
    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    9
    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG
    0

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA
    9
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    7

    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    43
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    45

    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    46
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    48

    yup2

    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    51

    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    52
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    3
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    1

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    6
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    6
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8
    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    59

    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    4
    ['pro', 'por', 'rpo', 'rop', 'opr', 'orp']
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    62
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    63
    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    8
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    65

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    0
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    67

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
    0
    from itertools import permutations
    
    words = [''.join(p) for p in permutations('pro')]
    
    print(words)
    5
    yup
    ypu
    uyp
    upy
    puy
    pyu
    None
    70

    Output:

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

    Độ phức tạp về thời gian: O (n!) Trong đó n là kích thước của chuỗi.AUXILIARY: O (n!) & NBSP; O(n!) where n is the size of the string.
    Auxiliary Space: O(n!) 


    Làm thế nào để bạn tìm thấy các hoán vị chuỗi trong Python?

    Để tìm tất cả các hoán vị có thể có 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.

    Hoán vị của chuỗi 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!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!

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

    Thuật toán cho hoán vị của một chuỗi trong java, trước tiên chúng ta sẽ lấy ký tự đầu tiên từ chuỗi và hoán vị với các ký tự còn lại.Nếu chuỗi = abc abc đầu tiên char = A và các hoán vị ký tự còn lại là BC và CB.Bây giờ chúng ta có thể chèn char đầu tiên vào các vị trí có sẵn trong các hoán vị.first take the first character from the String and permute with the remaining chars. If String = “ABC” First char = A and remaining chars permutations are BC and CB. Now we can insert first char in the available positions in the permutations.

    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).