日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

查找Python中给定字符串的所有排列

發布時間:2025/3/11 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 查找Python中给定字符串的所有排列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python itertools Module

Python itertools模塊

"itertools" are an inbuilt module in Python which is a collection of tools for handling iterators. It is the most useful module of Python. Here, a string is provided by the user and we have to print all the possible permutations of the given string in Python. As we all know the permutation is a way of arranging the elements of a group or set in a specific order or sequence which makes a different group. We all have calculated the permutations by using the pen and paper but here we will do this by using the Python programming language in a very small time.

“ itertools”是Python中的內置模塊,是處理迭代器的工具集合。 它是Python最有用的模塊。 在這里,用戶提供了一個字符串,我們必須在Python中打印給定字符串的所有可能排列 。 眾所周知,置換是按照組成不同組的特定順序或序列排列組或集合的元素的一種方式。 我們所有人都使用筆和紙計算了排列,但是在這里我們將在很短的時間內使用Python編程語言來完成排列。

Algorithm to solve the problem:

解決問題的算法:

  • Initially, we will import the permutation function from the itertools module.

    最初,我們將從itertools模塊導入置換函數。

  • Take the string from the user and assign it in a variable s.

    從用戶處獲取字符串,并將其分配給變量s 。

  • Generate all permutation using the permutation function and assign it in a variable p.

    使用置換函數生成所有置換,并將其分配給變量p 。

  • Since all elements of p are in tuple form. So, convert it in the list.

    由于p的所有元素都是元組形式。 因此,將其轉換為列表。

  • At last, add all list elements and print it which is our possible permutations.

    最后,添加所有列表元素并打印出來,這是我們可能的排列方式。

  • Let's start writing the Python program by implementing the above algorithm in a simple way.

    讓我們通過簡單的方法來實現上述算法,開始編寫Python程序。

    Python程序查找給定字符串的所有排列 (Python program to find all permutations of a given string)

    # importing the module from itertools import permutations# input the sting s=input('Enter a string: ')A=[] b=[] p=permutations(s)for k in list(p):A.append(list(k))for j in A:r=''.join(str(l) for l in j)b.append(r)print('Number of all permutations: ',len(b)) print('All permutations are: ') print(b)

    Output

    輸出量

    Enter a string: ABC Number of all permutations: 21 All permutations are: ['ABC', 'ABC', 'ACB', 'ABC', 'ACB', 'BAC', 'ABC', 'ACB', 'BAC', 'BCA', 'ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']

    翻譯自: https://www.includehelp.com/python/find-all-permutations-of-a-given-string.aspx

    總結

    以上是生活随笔為你收集整理的查找Python中给定字符串的所有排列的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。