def permute(lst): if len(lst)== 1: yield lst else: head = lst [:1] for x in permute(lst [1:]): yield head + x 收益率x +头部 返回 - Talin 解决方案 在文章< ma *************************************** @ python。 org>, Talin< ta *** @ acm.org>写道: 我确定我不是第一个这样做的人,但我想分享这个:一个返回列表所有排列的生成器: def permute(lst):如果len(lst)== 1:产量lst 否则: head = lst [:1] for permute(lst [1:]):产量头+ x 产量x +头返回 你是对的,你不是第一个这样做的人:很多其他人 也发布了不正确的排列生成器。 您是否在一些简单的测试用例上尝试过代码? list(permute([1,2,3])) ==> [[1,2,3],[2,3,1],[1,3,2],[3,2,1]] 明显缺席此列表是[2,1,3]和[2,3,1]。问题 因列表更长而变得更糟。基本问题是x需要能够在所有位置发生,而不仅仅是开始和结束。 干杯, -M - Michael J. Fromberger |计算机科学系讲师 http://www.dartmouth.edu / ~sting / |达特茅斯学院,美国新罕布什尔州汉诺威 Talin< ta *** @ acm.org>写道:我确定我不是第一个这样做的人,但我想分享这个:一个返回列表所有排列的生成器: def permute(lst):如果len(lst)== 1:产量lst 否则: head = lst [:1] 对于x in permute(lst [1:]):产量头+ x 产量x +头返回 - Talin 嗯: 用于置换p([1,2, 3]): 打印p [1,2,3] [2,3,1] [1,3,2] [3,2,1] 哎呀。 " Talin" < TA *** @ acm.org>在留言中写道 新闻:ma ************************************ *** @ pyt hon.org ... 我想分享这个:返回列表所有排列的生成器: 试试这个: def permuteg(lst):return([lst [i]] + x for i in range(len(lst)) for x in permute(lst [:i] + lst [i + 1:]))\ 或[[]] Alan Isaac I''m sure I am not the first person to do this, but I wanted to sharethis: a generator which returns all permutations of a list: def permute( lst ):if len( lst ) == 1:yield lstelse:head = lst[:1]for x in permute( lst[1:] ):yield head + xyield x + headreturn -- Talin 解决方案 In article <ma***************************************@python. org>,Talin <ta***@acm.org> wrote: I''m sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head = lst[:1] for x in permute( lst[1:] ): yield head + x yield x + head return You''re right that you''re not the first person to do this: Many othershave also posted incorrect permutation generators. Have you tried your code on some simple test cases? list(permute([1, 2, 3]))==> [[1, 2, 3], [2, 3, 1], [1, 3, 2], [3, 2, 1]] Notably absent from this list are [2, 1, 3] and [2, 3, 1]. The problemgets worse with longer lists. The basic problem is that x needs to beable to occur in ALL positions, not just the beginning and the end. Cheers,-M --Michael J. Fromberger | Lecturer, Dept. of Computer Science http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USATalin <ta***@acm.org> writes: I''m sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head = lst[:1] for x in permute( lst[1:] ): yield head + x yield x + head return -- Talin Hmm: for p in permute([1,2,3]):print p [1, 2, 3][2, 3, 1][1, 3, 2][3, 2, 1] Oops."Talin" <ta***@acm.org> wrote in messagenews:ma***************************************@pyt hon.org... I wanted to share this: a generator which returns all permutations of a list:Try this instead:def permuteg(lst): return ([lst[i]]+xfor i in range(len(lst))for x in permute(lst[:i]+lst[i+1:])) \or [[]] Alan Isaac 这篇关于置换发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 23:04