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

查找Python中给定字符串的所有排列相关推荐

  1. 在Python中给定包含该项目的列表的情况下查找项目的索引

    对于列表["foo", "bar", "baz"]和列表"bar" ,如何在Python中获取其索引(1)? #1楼 a ...

  2. Python中的字符串操作总结(Python3.6.1版本)

    Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...

  3. 如何在Python中获取字符串的子字符串?

    有没有一种方法可以在Python中为字符串加上字符串,以从第三个字符到字符串的末尾获取新的字符串? 也许像myString[2:end] ? 如果离开第二部分意味着"直到最后",而 ...

  4. python中定义字符串_python中的字符串

    python中的字符串一旦定义,则不可以修改 python中的原始字符串 即 在字符串前面加小写字母r 比如:打印输出C:\Program Files\Microsoft Games python中的 ...

  5. python中的字符串是什么,一文秒懂Python中的字符串

    摘要:本文将告诉您Python中的字符串是什么,并向您简要介绍有关该概念的所有知识. 因此,让我们开始吧. 什么是Python中的字符串? 我们许多熟悉C,C ++等编程语言的人都会得到诸如" ...

  6. 知道Python中的字符串是什么吗?

    什么是Python中的字符串? 我们许多熟悉C,C ++等编程语言的人都会得到诸如"字符串是字符的集合或字符数组"的答案. 在Python中也是如此,我们说的是String数据类型 ...

  7. 如何在Python中拆分字符串

    使用字符串时,常见的操作之一是使用给定的分隔符将字符串拆分为子字符串数组.在本文中,我们将讨论如何在Python中拆分字符串. PYthon学习企鹅裙:88198-2657 领取python自动化编程 ...

  8. Python基础与拾遗2:Python中的字符串与字符串格式化

    Python基础与拾遗2:Python中的字符串与字符串格式化 常见的字符串常量与表达式 字符串转义 索引和分片 原处修改字符串 字符串格式化 格式化表达式 格式化调用方法 添加具体格式化 本篇博文, ...

  9. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

最新文章

  1. JSR 133 Java内存模型以及并发编程的最权威论文汇总
  2. 智慧城市 android,基于Android系统的智慧城市服务客户端的设计与实现
  3. 又到校招季,来说说面试和实习
  4. java 反正多次重复提交_java web开发时防止刷新后的重复提交
  5. 库存管理-历史库存和收发存系列-MB5B
  6. 微信小程序 PDF下载打印
  7. Git 企业中常用分支管理策略
  8. SchemaCrawler 9.3 发布
  9. ubuntu 常用软件和命令
  10. Xen虚拟机两大迁移方法详解
  11. Linux shell编程学习笔记---第三章
  12. java同事只有竞争吗_如何防止同事竞争破坏公司文化
  13. ad网络标号设置经验_PLC模拟量通道设置的平均次数是什么意思?如何确定?
  14. linux合并第一列相同的行,linux – 合并重复的行并在最后添加“N / A...
  15. python thread 多线程
  16. Windows平台上安装搭建iPhone/iPad的开发环境
  17. ~~Trie树(数据结构)(附题目:AcWing 835. Trie字符串统计)
  18. 正则表达式教程之模式修正符
  19. 蓝桥杯 ALGO-107 算法训练 9-7链表数据求和操作
  20. Zabbix中文使用手册

热门文章

  1. ros如何编译python文件_Python为ROS编写一个简单的发布者和订阅者
  2. flutter 分割线_Flutter 底部弹框 showModalBottomSheet 使用Demo
  3. 大物实验总结模板_期中总结大会amp;期末动员大会
  4. (2021) 22 [持久化] 1-Bit的存储
  5. java分布式会话redis_详解springboot中redis的使用和分布式session共享问题
  6. mysql核心参数_MySQL技术体系之核心参数
  7. python界面开发webview_python在webview和activity混合页面下怎么定位
  8. Java并发篇_乐观锁与悲观锁
  9. 新手教程:不写JS,在MIP页中实现异步加载数据
  10. mip-link 组件功能升级说明