Python论坛  - 讨论区

标题:[python-chinese] 排列组合算法的回帖

2006年04月16日 星期日 09:54

shhgs shhgs.efhilt at gmail.com
Sun Apr 16 09:54:33 HKT 2006

上个月给cookbook发了一个排列组合算法的程序

Raymond Hattinger发了一个评论。惭愧,惭愧。水平高低真是一目了然。

----------------------------------------------------------------------------------------------------
Much simpler and clearer version, Raymond Hettinger, 2006/03/23

def perm(items, n=None):
    if n is None:
        n = len(items)
    for i in range(len(items)):
        v = items[i:i+1]
        if n == 1:
            yield v
        else:
            rest = items[:i] + items[i+1:]
            for p in perm(rest, n-1):
                yield v + p

def comb(items, n=None):
    if n is None:
        n = len(items)
    for i in range(len(items)):
        v = items[i:i+1]
        if n == 1:
            yield v
        else:
            rest = items[i+1:]
            for c in comb(rest, n-1):
                yield v + c
----------------------------------------------------------------------------------------------------

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月16日 星期日 19:58

Z Zarz zarz.ml at gmail.com
Sun Apr 16 19:58:44 HKT 2006

能解释一下算法吗?

在06-4-16,shhgs <shhgs.efhilt at gmail.com> 写道:
>
> 上个月给cookbook发了一个排列组合算法的程序
>
> Raymond Hattinger发了一个评论。惭愧,惭愧。水平高低真是一目了然。
>
>
> ----------------------------------------------------------------------------------------------------
> Much simpler and clearer version, Raymond Hettinger, 2006/03/23
>
> def perm(items, n=None):
>     if n is None:
>         n = len(items)
>     for i in range(len(items)):
>         v = items[i:i+1]
>         if n == 1:
>             yield v
>         else:
>             rest = items[:i] + items[i+1:]
>             for p in perm(rest, n-1):
>                 yield v + p
>
> def comb(items, n=None):
>     if n is None:
>         n = len(items)
>     for i in range(len(items)):
>         v = items[i:i+1]
>         if n == 1:
>             yield v
>         else:
>             rest = items[i+1:]
>             for c in comb(rest, n-1):
>                 yield v + c
>
> ----------------------------------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060416/2b240542/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月16日 星期日 20:54

shhgs shhgs.efhilt at gmail.com
Sun Apr 16 20:54:46 HKT 2006

Hattinger的算法确实简单,就是递归。

排列的时候,先按顺序取一个值,然后把剩下的放到rest里面,再用递归在rest里面取n-1个值,然后把它们加起来

组合也差不多,至少拿第一个值和计算rest的时候有些不一样

On 4/16/06, Z Zarz <zarz.ml at gmail.com> wrote:
> 能解释一下算法吗?
>
> 在06-4-16,shhgs <shhgs.efhilt at gmail.com> 写道:
> >
>  上个月给cookbook发了一个排列组合算法的程序
>
> Raymond Hattinger发了一个评论。惭愧,惭愧。水平高低真是一目了然。
>
> ----------------------------------------------------------------------------------------------------
> Much simpler and clearer version, Raymond Hettinger, 2006/03/23
>
> def perm(items, n=None):
>     if n is None:
>         n = len(items)
>     for i in range(len(items)):
>         v = items[i:i+1]
>         if n == 1:
>             yield v
>         else:
>             rest = items[:i] + items[i+1:]
>             for p in perm(rest, n-1):
>                 yield v + p
>
> def comb(items, n=None):
>     if n is None:
>         n = len(items)
>     for i in range(len(items)):
>         v = items[i:i+1]
>         if n == 1:
>             yield v
>         else:
>             rest = items[i+1:]
>             for c in comb(rest, n-1):
>                 yield v + c
> ----------------------------------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号