标准语法

itertools.permutations(iterable[, r])

含义

Return successive r length permutations of elements in the iterable.

If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

简单来说呢就是返回迭代器中元素的连续长度为r的排列。也就是全排列呗。
如果r未指定或为None,则r默认为可迭代对象的长度,并生成所有可能的全长度排列。

代码

def permutations(iterable, r=None):# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC# permutations(range(3)) --> 012 021 102 120 201 210pool = tuple(iterable)n = len(pool)r = n if r is None else rif r > n:returnindices = range(n)cycles = range(n, n-r, -1)yield tuple(pool[i] for i in indices[:r])while n:for i in reversed(range(r)):cycles[i] -= 1if cycles[i] == 0:indices[i:] = indices[i+1:] + indices[i:i+1]cycles[i] = n - ielse:j = cycles[i]indices[i], indices[-j] = indices[-j], indices[i]yield tuple(pool[i] for i in indices[:r])breakelse:return

仔细看这个代码,相信各位能看明白的哈。

算法示例

此处我们可以来看一下24点游戏的代码咯

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 20 23:29:53 2021@author: Administrator
"""
import itertools
def twentyfour(cards):for nums in itertools.permutations(cards):for ops in itertools.product('+-*/',repeat=3):#构造三种中缀表达式 bsdbds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops) #(a+b)*(c-d)bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops) #(a+b)*c-dbds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops) #a/(b-(c/d))for bds in [bds1,bds2,bds3]:try:if abs(eval(bds)-24.0)<1e-10:   #计算return bdsexcept ZeroDivisionError: #除零错误continuereturn 'Not fount'cards = [1,2,3,4]
for card in cards:print(twentyfour(card))

【python日用】itertools.permutations用法相关推荐

  1. permutations python_为什么Python的itertools.permutations包含重复项? (当原始列表重复时)...

    为什么Python的itertools.permutations包含重复项? (当原始列表重复时) 普遍认为,n个不同符号的列表有n! 排列. 但是,当符号不明确时,在math和其他地方最常见的惯例似 ...

  2. python中itertools的用法,【python日用】itertools.product用法

    标准语法 itertools.product(*iterables[, repeat]) 含义 Cartesian product of input iterables. Roughly equiva ...

  3. python中itertools的用法_python中的itertools的使用详解

    今天了解了下python中内置模块itertools的使用,熟悉下,看能不能以后少写几个for,嘿嘿

  4. 【python日用】itertools.product用法

    标准语法 itertools.product(*iterables[, repeat]) 含义 Cartesian product of input iterables. Roughly equiva ...

  5. python 彩票排列组合_对福彩3D号码进行排列组合为例学习Python的itertools模块的用法...

    这里我们以对福彩3D号码进行排列组合为例学习Python的itertools模块的用法.首先我们选择心仪的号码.比如我们选择4,5,7,8 第一种我们只要组六的组合.代码如下 import itert ...

  6. Python Itertools.chain()用法【将一组迭代对象串联起来,形成一个更大的迭代器】

    它是一个需要一系列可迭代对象并返回一个可迭代对象的函数.它将所有可迭代对象组合在一起,并生成一个可迭代对象作为输出. 场景一:  chain()可以把一组迭代对象串联起来,形成一个更大的迭代器: &g ...

  7. python itertools combination_Python itertools.combinations 和 itertools.permutations 等价代码实现...

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  8. python的itertools详解

    Python中的itertools模块是一个用于迭代工具的标准库.它包含了很多用于迭代处理的函数和生成器,可以让开发者更加方便地处理迭代任务. 以下是itertools模块的一些常用函数: itert ...

  9. python的itertools库_Python标准库itertools模块使用方法

    简介 官方描述:Functional tools for creating and using iterators.即用于创建高效迭代器的函数. itertools.chain(*iterable) ...

最新文章

  1. 【Web前端培训基础知识】ES5及ES6this详解
  2. openstack网络服务neutron
  3. 代码农民从做事情的经验
  4. 一个很不错的支持Ext JS 4的上传按钮
  5. java opencv 开发环境_Java + opencv学习:在Eclipse下配置基于Java的OpenCV开发环境
  6. 原生Get请求和Post请求
  7. 集成android studio,Android Studio集成
  8. 12v小型电机型号大全_鄂破碎机型号大全图,小型鄂破碎机价格
  9. C语言定义外部文件可使用的结构体和结构体变量
  10. python win32gui安装_python-无法安装win32gui
  11. D3.js 力导向图来处理拓扑图
  12. 【论文笔记】EMNLP2019: 基于层次多图卷积网络的实体类型分类
  13. mysql之使用json
  14. 服务器安装iis网站,安装IIS发布我的第一个网站图文教程
  15. 用英雄联盟的方式讲解 JavaScript 设计模式
  16. DAY07-ES5-String
  17. web端 小米商城网站总结
  18. SLAM--LSD_SLAM在高版本系统中运行(ubuntu20.04 ROS-noetic)
  19. 使用计算机管理文件教后反思,《管理计算机中的文件》教学设计
  20. 一步步学会用docker部署应用(nodejs版)

热门文章

  1. [过程挖掘 Process Mining] Demo scenario 演示场景(一)
  2. spring boot 开发笔记(二)
  3. 为什么 a[i]=*(a+i);
  4. inline函数返回值_C++ inline关键字详解
  5. 通用`Query`解决方案
  6. ubuntu Linux 20.04 dpkg安装vscode过程
  7. iOS开发实用技术之传感器
  8. 7-22 显示菱形图形 (40分)
  9. [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  10. if 0 endif if 1 endif