查找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:

解決問題的算法:

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

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

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

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

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

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

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

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

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541678.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541678.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541678.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

android 圖片疊加xml,Android實現圖片疊加效果的兩種方法

本文實例講述了Android實現圖片疊加效果的兩種方法。,具體如下:效果圖:第一種:第二種:第一種是通過canvas畫出來的效果:public void first(View v) {// 防止出現Immutable bitmap passed to Canvas constructor錯誤Bit…

Win10系列:VC++ 定時器

計時器機制俗稱"心跳",表示以特定的頻率持續觸發特定事件和執行特定程序的機制。在開發Windows應用商店應用的過程中,可以使用定義在Windows::UI::Xaml命名空間中的DispatcherTimer類來創建計時器。DispatcherTimer類包含了如下的成員&#xf…

dbms系統 rdbms_DBMS與傳統文件系統之間的區別

dbms系統 rdbmsIntroduction 介紹 DBMS and Traditional file system have some advantages, disadvantages, applications, functions, features, components and uses. So, in this article, we will discuss these differences, advantages, disadvantages and many other …

android 百度地圖api密鑰,Android百度地圖開發獲取秘鑰之SHA1

最近在做一個關于百度地圖的開發。不過在正式開發之前還必須要在百度地圖API官網里先申請秘鑰,而在申請秘鑰的過程中,就需要獲取一個所謂的SHA1值。如上所示,但是由于不是正式開發,所以以上的發布版和開發版的SHA1可以先填寫相同。…

單位矩陣的逆| 使用Python的線性代數

Prerequisites: 先決條件: Defining a Matrix 定義矩陣 Identity Matrix 身份矩陣 There are matrices whose inverse is the same as the matrices and one of those matrices is the identity matrix. 有些矩陣的逆與矩陣相同,并且這些矩陣之一是單位…

華為榮耀七能升級鴻蒙系統嗎,華為鴻蒙系統來了,你知道哪些華為手機榮耀手機可以升級嗎?...

從鴻蒙系統第一次開始登場,到現在慢慢有許多鴻蒙系統設備出現,手機市場的格局似乎又要升級變化了。科技樹兒了解到,在某數碼博主經過和相關人員的溝通核實之后,目前暫定的是搭載華為麒麟710芯片以上的機型,無論華為或榮…

day5-shutil模塊

一、簡述 我們在日常處理文件時,經常用到os模塊,但是有的時候你會發現,像拷貝、刪除、打包、壓縮等文件操作,在os模塊中沒有對應的函數去操作,下面我們就來講講高級的 文件、文件夾、壓縮包 處理模塊:shuti…

matlab中now函數_now()方法以及JavaScript中的示例

matlab中now函數JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is used to current time in milliseconds, it returns the total number of milliseconds since 01st January 1970, 00:00:00 UTC. now()方法是Date類的一種方法&am…

android 集成x5內核時 本地沒有,騰訊瀏覽服務-接入文檔

三、SDK集成步驟1. 第一步下載 SDK jar 包放到工程的libs目錄下,將源碼和XML里的系統包和類替換為SDK里的包和類,具體對應如下:系統內核SDK內核android.webkit.ConsoleMessagecom.tencent.smtt.export.external.interfaces.ConsoleMessageand…

java vector_Java Vector sureCapacity()方法與示例

java vector向量類別sureCapacity()方法 (Vector Class ensureCapacity() method) ensureCapacity() method is available in java.util package. sureCapacity()方法在java.util包中可用。 ensureCapacity() method is used to ensure the capacity of this Vector when requi…

Tcl與Design Compiler (十二)——綜合后處理

本文如果有錯,歡迎留言更正;此外,轉載請標明出處 http://www.cnblogs.com/IClearner/ ,作者:IC_learner 概述 前面也講了一些綜合后的需要進行的一些工作,這里就集中講一下DC完成綜合了,產生了…

Java短類的compareTo()方法和示例

簡短的類compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Short object against the given Short obj…

四則運算網頁版

一.設計思想: 1)寫出一個菜單界面,有兩個選項一個是分數,一個是整數。 2)而這兩個標簽后面則是轉向其更詳細的菜單,題目數量,有無括號,運算的項數等等詳細功能,再點擊這兩…

Java RandomAccessFile seek()方法與示例

RandomAccessFile類seek()方法 (RandomAccessFile Class seek() method) seek() method is available in java.io package. seek()方法在java.io包中可用。 seek() method is used to sets the file pointer position calculated from the starting of this file at which the …

Javascript開發技巧(JS中的變量、運算符、分支結構、循環結構)

一、Js簡介和入門 繼續跟進JS開發的相關教程。 <!-- [使用JS的三種方式] 1、HTML標簽中內嵌JS&#xff08;不提倡使用&#xff09;&#xff1a; 示例&#xff1a;<button οnclick"javascript:alert(你真點啊&#xff01;)">有本事點我呀&#xff01;&#…

android 顏色范圍,Android系統顏色的適用范圍

###All Clickable Views:ripple effect (Lollipop only) — “colorControlHighlight”###Status Bar:background (Lollipop only) – “colorPrimaryDark”###Navigation Bar:background (Lollipop only) – “android:navigationBarColor”###EditText:underline (unfocused)…

bytevalue_Java Short類byteValue()方法及示例

bytevalue短類byteValue()方法 (Short class byteValue() method) byteValue() method is available in java.lang package. byteValue()方法在java.lang包中可用。 byteValue() method is used to return the value denoted by this Short object converted to type byte (by …

分布式交換機配置備份和還原

1.備份和還原vSphere Distributed Switch配置 1.1導出 vSphere Distributed Switch 配置 可以將 vSphere Distributed Switch 和分布式端口組配置導出到某一文件。該文件保留有效的網絡配置&#xff0c;使這些配置能夠傳輸至其他環境。 步驟&#xff1a; 1) 在 vSphere Web Cli…

html自動執行函數,JS 自執行函數原理及用法

js自執行函數&#xff0c;聽到這個名字&#xff0c;首先會聯想到函數。接下來&#xff0c;我來定義一個函數&#xff1a;function aaa(a,b){return sum a b}定義了一個名為aaa的函數&#xff0c;在里面可以計算兩個數的和。如果想執行它&#xff0c;就必須得調用它&#xff0…

java reverse_Java Integer類reverse()方法與示例

java reverse整數類reverse()方法 (Integer class reverse() method) reverse() method is available in java.lang package. reverse()方法在java.lang包中可用。 reverse() method is used to returns the value generated by reversing the order of bits in binary 2s comp…