2006年04月16日 星期日 09:54
上个月给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
----------------------------------------------------------------------------------------------------
2006年04月16日 星期日 19:58
能解释一下算法吗? 在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
2006年04月16日 星期日 20:54
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 > >
Zeuux © 2025
京ICP备05028076号