查找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.mzph.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…