玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python生成0-9任意4位数字组合的方法

python 培训

玩蛇网本文是关于Python生成0-9任意4位数字组合的方法示例。0-9个数字组成任意4位数字,有多少种排列组合的方式,要是用想的需要不少时间而且还容易出错。用python方法来解决这个问题显然简单得多。只需要一段代码,就可以轻松解决。Python生成四位不重复数字(0-9)的所有组合,要如何操作请往下看。

Python生成0-9任意4位数字组合的方法,Python源代码示例如下:(供参考)

def init_set8(r10=range(10)):
    """
    把循环内的range函数提到外面
    times5.486 ==> 4.427
    """
    ret = []
    for i in r10:
        for j in r10:
            for k in r10:
                for l in r10:
                    if i != j and i != k and i != l and j != k and j != l and k != l:
                        ret.append((i, j, k, l))
    return ret

timing(init_set8, 1000)

def init_set9(r10=range(10)):
    """
    for 循环改成列表推导
    times5.486 ==>3.773 
    """
    return [(i, j, k, l)
        for i in r10
        for j in r10
        for k in r10
        for l in r10
        if ( i != j and i != k and i != l and j != k and j != l and k != l) ]
timing(init_set9, 1000)

def init_set10(r10=range(10)):

    return ((i, j, k, l)
            for i in r10
            for j in r10
            for k in r10
            for l in r10
            if( i != j and i != k and i != l and j != k and j != l and k != l) )
timing(init_set10, 1000)

def init_set11():
    """
    用代码的空间代价换取计算P4_4的时间
    init_set11 1000 times 7.268 OMG
    reduce(lambda x,y:x+y,l)太慢了
    """
    c10_4=[( i, j, k, l ) for i in xrange(0, 10)
                          for j in xrange(i+1, 10)
                          for k in xrange(j+1, 10)
                          for l in xrange(k+1, 10) ]

    ret=reduce(lambda x,y:x+y,
            [ [ (i, j, k, l),
                (i, j, l, k),
                (i, k, j, l),
                (i, k, l, j),
                (i, l, j, k),
                (i, l, k, j),
                (j, i, k, l),
                (j, i, l, k),
                (j, k, i, l),
                (j, k, l, i),
                (j, l, i, k),
                (j, l, k, i),
                (k, i, j, l),
                (k, i, l, j),
                (k, j, i, l),
                (k, j, l, i),
                (k, l, i, j),
                (k, l, j, i),
                (l, i, j, k),
                (l, i, k, j),
                (l, j, i, k),
                (l, j, k, i),
                (l, k, i, j),
                (l, k, j, i),]
                for i, j, k, l in c10_4 ],
            ) 
    return ret

def init_set12():
    """
    generator是伟大的发明,数据流编程万岁
    init_set12 1000 times 1.758 
    www.iplaypython.com

    """
    c10_4=(( i, j, k, l ) for i in xrange(0, 10)
            for j in xrange(i+1, 10)
            for k in xrange(j+1, 10)
            for l in xrange(k+1, 10) )

    from  itertools import chain
    ret=chain(
        *( ( (i, j, k, l),
             (i, j, l, k),
             (i, k, j, l),
             (i, k, l, j),
             (i, l, j, k),
             (i, l, k, j),
             (j, i, k, l),
             (j, i, l, k),
             (j, k, i, l),
             (j, k, l, i),
             (j, l, i, k),
             (j, l, k, i),
             (k, i, j, l),
             (k, i, l, j),
             (k, j, i, l),
             (k, j, l, i),
             (k, l, i, j),
             (k, l, j, i),
             (l, i, j, k),
             (l, i, k, j),
             (l, j, i, k),
             (l, j, k, i),
             (l, k, i, j),
             (l, k, j, i),)
            for i, j, k, l in c10_4 )
        )  
    return list(ret)
timing(init_set12, 1000)

Python算法相关文章推荐:
1、Python组合生成与数量计算的实现方法
2、Python常见排序算法实现与测速源码

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/algorithm/a2589.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-19 19:24 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部