简体   繁体   English

列出所有可能的组合

[英]Listing all possible combinations

I am wondering if you can lend me a hand in this issue please. 我想知道您是否可以帮我解决这个问题。

I have the following code to represent all possible combination of range of numbers: 我有以下代码代表数字范围的所有可能组合:

import itertools
lst = [1, 2, 3]
combs = []
for i in xrange(1, len(lst)+1):
   els = [list(x) for x in itertools.combinations(lst, i)]
   combs.extend(els)

The thing is it represent the output in form of 问题是它以以下形式表示输出

[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

It would be great benefit for me to represent each combination on a separate text files. 对我来说,将每个组合表示在单独的文本文件中将是非常有益的。 Each text file represent each number in the combination in a single line. 每个文本文件在一行中代表组合中的每个数字。 As (1,2), 1 to be in the first line and 2 to be in the seconds line without any commas. 与(1,2)一样,1位于第一行,2位于秒行,没有逗号。

I would deeply appreciate your kindness in helping me. 衷心感谢您对我的帮助。

Edit 编辑

Thanks million guys for your help. 感谢数以百万计的人的帮助。 Do appreciate it. 感谢它。

I still have a small issue to solve here please. 我还有一个小问题要解决。

For Poke solution, which is great, there is a small problem (my mistake of illustrating this) 对于Poke解决方案,这很好,这是一个小问题(我在说明问题时的错误)

the output of the file would be: 该文件的输出为:

[[1], [2], [3]]
[[1, 2], [1, 3], [2, 3]]
[[1, 2, 3]]

The thing is I need to use the code for LARGE number of combinations (6-39). 事情是我需要使用代码的大数目的组合(6-39)。

Can you help me with this? 你能帮我吗? Even if editing the provided code itself? 即使自己编辑提供的代码?

Million thanks in advance 预先感谢一百万

Your issue is completely separate from actually generating the combinations. 您的问题与实际生成组合完全不同。 What you want is just a special way to output your data. 您想要的只是一种输出数据的特殊方式。 And you could have done that easily using standard file writing stuff: 使用标准的文件编写工具,您可以轻松地做到这一点:

>>> combinations = [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
>>> for i, combination in enumerate(combinations):
        with open(r'C:\Users\poke\Desktop\foobar\{0}.txt'.format(i), 'w+') as f:
            for value in combination:
                f.write(str(value) + '\n')
import itertools

def savecomb(a, basename):
    k = 0
    for n in range(1, len(a) + 1):
        for c in itertools.combinations(a, n):
            k += 1
            f = open("{}{}.txt".format(basename, k), "wt")
            for i in c:
                f.write("{}\n".format(i))
            f.close()

There is another solution, using powerset, which is not defined in itertools, but described here (look for powerset in the page): 还有另一种使用powerset的解决方案,该解决方案未在itertools中定义,但在此处进行了描述(请在页面中查找powerset ):

def powerset(iterable):
    s = list(iterable)
    return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))

def savecomb(a, basename):
    for k, c in enumerate(powerset(a)):
        with open("{}{}.txt".format(basename, k), "wt") as f:
            for i in c:
                f.write("{}\n".format(i))

This works because taking combinations of all sizes is exactly the same thing as taking all subsets of your list. 之所以可行,是因为采用所有大小的组合与采用列表的所有子集是完全一样的。 There will be an empty file, which accounts for the empty subset. 将有一个空文件,占空子集。

Also, bear in mind that there are 2^n subsets, where n is the size of the list, so for even not too large n, there will be many files. 另外,请记住,有2 ^ n个子集,其中n是列表的大小,因此即使n不太大,也会有很多文件。 In such situation, even if disk space is large enough, there may be a problem with the filesystem which won't like having too many files in a directory. 在这种情况下,即使磁盘空间是足够大的,有可能是其不喜欢在一个目录有太多的文件的文件系统有问题。 So it may be wise to put them in different directories (which needs adapting the code slightly), or better, to resort to another approach. 因此,将它们放在不同的目录中(明智的做法是稍微修改一下代码)可能是明智的选择,或者最好使用另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM