Python all combinations of data
Question: So I have a file structure like this _(data on each line)_ :

    
    
    A
    B
    C
    ...
    

I want to read each line of this file and combine those into all possible
combinations, the result I'm looking for would be this _(which I would put in
a list)_ :

    
    
    A
    AB
    AC
    ABC
    ACB
    B
    BA
    BC
    BCA
    BAC
    C
    CA
    CB
    CBA
    CAB
    

Obviously I'd be looking to deal with more than just those three lines...

thanks


Answer: You should use the `itertools` library. You want to generate all unique
permutations of each element in the powerset. Some code might look like

    
    
    from itertools import permutations, combinations, chain
    
    # Taken from itertools page, but edited slightly to not return empty set
    def powerset(iterable):
        "powerset([1,2,3]) --> (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
        s = list(iterable)
        return chain.from_iterable(combinations(s, r) for r in range(1, len(s)+1))
    

Then

    
    
    In [1]: s = ('A', 'B', 'C')
    
    In [2]: [j for i in powerset(s) for j in permutations(i)]
    Out[2]: 
    [('A',),
     ('B',),
     ('C',),
     ('A', 'B'),
     ('B', 'A'),
     ('A', 'C'),
     ('C', 'A'),
     ('B', 'C'),
     ('C', 'B'),
     ('A', 'B', 'C'),
     ('A', 'C', 'B'),
     ('B', 'A', 'C'),
     ('B', 'C', 'A'),
     ('C', 'A', 'B'),
     ('C', 'B', 'A')]
    

