zoukankan      html  css  js  c++  java
  • Python排列组合实验

    import itertools  
    
    排列: 4个数内选2个
    >>> print list(itertools.permutations([1,2,3,4],2))
    [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
    
    组合:4个数内选2个:
    >>> print list(itertools.combinations([1,2,3,4],2))
    [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
    
    _____________________________________
    
    ABCDE 5个数的排列(去掉重复):
    >>>len(set(list(itertools.permutations(['A','B','C','D','E'],5))))
    120
    
    AABCD 5个数不同的排列(去掉重复):
    >>>len(set(list(itertools.permutations(['A','A','B','C','D'],5))))
    60
    
    AABBC(去掉重复):
    >>> len(set(list(itertools.permutations(['A','A','B','B','C'],5))))
    30
    
    AAABC(去掉重复):
    >>> len(set(list(itertools.permutations(['A','A','A','B','C'],5))))
    20
    
    AAABB(去掉重复):
    >>> len(set(list(itertools.permutations(['A','A','A','B','B'],5))))
    10
    
    AAAAB(去掉重复):
    >>> len(set(list(itertools.permutations(['A','A','A','A','B'],5))))
    5
    import math
    
    //求阶乘
    math.factorial(3)
    
    //求排列数 : n个数选x个
    def P(n,x):
    	return math.factorial(n)/math.factorial(n-x)
    	
    //求组合数 : n个数选x个
    def C(n,x):
    	return P(n,x) / math.factorial(x)
    
    一手牌满堂红的概率:(5张牌,3张点相同,另2张点数相同)
    P(13,2) * C(4,3) * C(4,2) / C(13*4,5) = 0.0014405762304921968
    

      

  • 相关阅读:
    解决Eclipse中文乱码
    C++中set用法回顾
    二分查找题目汇总
    给网卡配置多个IP地址(win/linux)
    route在windows与liunx下的使用区别
    eclipse 编程 c++ 快捷键
    Git、GitHub、GitLab三者之间的联系以及区别
    SQL语句优化
    《领域驱动设计的原则与实践》读书笔记(一)
    DotNet Core 介绍
  • 原文地址:https://www.cnblogs.com/wucg/p/4571382.html
Copyright © 2011-2022 走看看