• python 实现排列组合


    对于一个数组(或任何可以迭代的元素集),可以通过itertools包中的permutationscombinations轻松完成排列,组合

    python3中permutationscombinations返回的是一个迭代器,可以通过list转化为一个列表,方便我们进一步处理

    具体用法看下面的例子

    import itertools
    from itertools import permutations, combinations
    
    print("Permutations of 'bar'")
    print(list(permutations('bar')))
    
    #输出结果
    #Permutations of 'bar'
    #[('b', 'a', 'r'), ('b', 'r', 'a'), ('a', 'b', 'r'), ('a', 'r', 'b'), ('r', 'b', 'a'), ('r', 'a', 'b')]
    
    print("Combinations of 2 letters from 'bar'")
    print(list(combinations('bar', 2)))
    
    #输出结果
    #Combinations of 2 letters from 'bar'
    #[('b', 'a'), ('b', 'r'), ('a', 'r')]
  • 相关阅读:
    10.31JS日记
    10.24JS日记
    10.23JS日记
    10.22JS日记
    10.19JS日记
    10.18JS日记
    Tomcat—Bad Request
    2016年上半年总结
    线程间操作无效
    压缩字符串的函数
  • 原文地址:https://www.cnblogs.com/RB26DETT/p/11734236.html
Copyright © 2020-2023  润新知