简体   繁体   中英

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.

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)

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).

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):

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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