在此之前,从这样一个问题:
算法以生成跨越设置
鉴于此输入:[1,2,3,4]
我想产生这样一套套的Python:
[1] [2] [3] [4]
[1] [2] [3,4]
[1] [2,3,4]
[1] [2,3] [4]
并[1,2] [3] [4]
并[1,2] [3,4]
[1,2,3] [4]
[1,2,3,4]
所以不像previous问题,列表的顺序保持不变。
理想情况下,code将工作n项列表中的
非常感谢
编辑2:任何人都可以告诉我如何做到这一点,如果原来的输入是一个字符串,而不是一个列表(其中字符串中的每个单词变为列表中的一个项目)。谢谢!
编辑:添加的[1] [2,3,4]对不起,我错
解决方案您可能也喜欢用递归方法解决:
高清跨度(LST):
产量[LST]
因为我在范围内(1,LEN(LST)):
对于x跨度(LST [我:]):
产生[LST [我] + X
我们这里的递归利用突破的问题了。该方法如下:
对于每一个列表,整个列表是一个有效的跨越: [1,2,3,4] => [1,2,3,4]]
。
对于每一个列表长于尺寸 1
,我们可以使用的第一个项目为一组,然后采用相同的算法,剩下的名单上,让所有的组合结果:
[1,2,3] =>
[[1]] + [[2],[3]]#=> [1],[2],[3]]
[[1]] + [[2,3]]#=> [1],[2,3]]
对于每一个列表长于尺寸 2
,我们可以只以及使用第一个两个项为一组,然后应用同样的算法,剩下的名单上,合并结果:
[1,2,3,4,5] =>
[[1,2] + [[3],[4],[5]]#=> [[1,2],[3],[4],[5]]
[[1,2] + [[3,4],[5]]#=> [[1,2],[3,4],[5]]
[[1,2] + [[3],[4,5]]#=> [[1,2],[3],[4,5]]
[[1,2] + [[3,4,5]]#=> [[1,2],[3,4,5]
我们可以看到,在右侧的可能组合是确实的所有清单的剩余部分的可能的分组, [3,4,5]
对于每一个列表的长度超过...等。因此,最终的算法如下:
得到整个列表(它始终是一个有效的跨越,见上文) 对于列表的每一个可能的拆分,得到结合在列表的右边部分的所有可能spannings列表的左手部分。 收益率
在Python中一个特殊的关键字,使功能的发电机的,这意味着它返回一个可用于一个迭代的对象列举找到的所有结果。您可以使用列表转换结果成一个列表
构造函数:列表(量程([1,2,3,4]))
。
This follows on from this question:
Algorithm to generate spanning set
Given this input: [1,2,3,4]
I'd like to generate this set of sets in python:
[1] [2] [3] [4]
[1] [2] [3,4]
[1] [2, 3, 4]
[1] [2,3] [4]
[1,2] [3] [4]
[1,2] [3,4]
[1,2,3] [4]
[1,2,3,4]
So unlike the previous question, the order of the list is retained.
Ideally the code would work for n items in the list
Thanks very much
EDIT 2: Could anyone advise me on how to do this if the original input is a string rather than a list (where each word in the string becomes an item in a list). Thanks!
EDIT: added [1] [2, 3, 4] Sorry for the mistake
解决方案You might also enjoy a recursive solution:
def span(lst):
yield [lst]
for i in range(1, len(lst)):
for x in span(lst[i:]):
yield [lst[:i]] + x
We exploit recursion here to break the problem down. The approach is the following:
For every list, the whole list is a valid spanning: [1,2,3,4] => [[1,2,3,4]]
.
For every list that is longer than size 1
, we can use the first item as a group and then apply the same algorithm on the remaining list to get all the combined results:
[1,2,3] =>
[[1]] + [[2], [3]] # => [[1], [2], [3]]
[[1]] + [[2,3]] # => [[1], [2,3]]
For every list that is longer than size 2
, we can just as well use the first two items as a group and then apply the same algorithm on the remaining list and combine the results:
[1,2,3,4,5] =>
[[1,2]] + [[3], [4], [5]] # => [[1,2], [3], [4], [5]]
[[1,2]] + [[3,4], [5]] # => [[1,2], [3,4], [5]]
[[1,2]] + [[3], [4,5]] # => [[1,2], [3], [4,5]]
[[1,2]] + [[3,4,5]] # => [[1,2], [3,4,5]]
We can see that the possible combinations on the right side are indeed all possible groupings of the remainder of the list, [3,4,5]
.
For every list that is longer than ... etc. Thus, the final algorithm is the following:
yield the whole list (it is always a valid spanning, see above) For every possible splitting of the list, yield the left-hand part of the list combined with all possible spannings of the right-hand part of the list.yield
is a special keyword in Python that make the function a generator, which means that it returns a iterable object that can be used to enumerate all results found. You can transform the result into a list using the list
constructor function: list(span([1,2,3,4]))
.