Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python count all possible combinations for a table

I have a table that looks like below:

   PotA  PotB  PotC  PotD  PotE
A   +     +     +     +     +
B   -     ?     +     +     ?
C   +     +     +     +     +
D   +     -     +     -     +
E   +     +     +     +     +

From here, I have to find all the possible combinations of "+","-" and "?" for all combinations of (PotA and PotB), (PotA and PotC), and so on, (PotA, PotB and PotC), and finally to (PotA, PotB, PotC, PotD and PotE). Actually the "Pot" row keeps going on, but here I only show till PotE for simplification.

To do this, first, I read the file like as follows, and then, generate all possible possibilities for a combination of two for count each possibility.

def readDatafile():
    filename = ("data.txt")
    infile = open(filename,'r')

    for line in infile.readlines():
        line = line.strip()
        print (line)          # just to check the data, need to carry on from here.

"""Generate all possible permutations for later count"""
def doPermutations(items, n):
    if n == 0:
        yield ''
    else:
        for i in range(len(items)):
            for base in doPermutations(items, n - 1):
                yield str(items[i]) + str(base)

def makeAllPossibleList():
    nlength      = 2          # This should go outside of the function and will be same as the number of Pots
    lpossibility = ['+', '-', '?']
    litems       = []

    for i in doPermutations(lpossibility, int(nlength)):
        litems.append(i)

    for x in items:
        print (x)             # This generate all the possible items for combination of two

So, the end result would be like this:

Combination: Possibility Count
PotA, PotB: ++ 3
PotA, PotB: +- 1
PotA, PotB: +? 0
PotA, PotB: -+ 0
PotA, PotB: -- 0
PotA, PotB: -? 1
PotA, PotB: ?+ 0
PotA, PotB: ?- 0
PotA, PotB: ?? 0
PotA, PotC: ...
PotA, PotC: ...
.......
PotA, PotB, PotC, PotD, PotE: +++++ 3
PotA, PotB, PotC, PotD, PotE: ++++- 0
PotA, PotB, PotC, PotD, PotE: ++++? 0
.......

Is there any good python method to get the proper logic for this problem? Do I have to read the data with the headers as keys and columns as value of a list?

I cannot get a proper logic. Please give me some help.

like image 620
Karyo Avatar asked Nov 26 '13 10:11

Karyo


1 Answers

Assuming I understand what you're after, how about something like:

import itertools
import collections

def read_table(filename):
    with open(filename) as fp:
        header = next(fp).split()
        rows = [line.split()[1:] for line in fp if line.strip()]
        columns = zip(*rows)
    data = dict(zip(header, columns))
    return data

table = read_table("data.txt")
pots = sorted(table)

alphabet = "+-?"
for num in range(2, len(table)+1):
    for group in itertools.combinations(pots, num):
        patterns = zip(*[table[p] for p in group])
        counts = collections.Counter(patterns)
        for poss in itertools.product(alphabet, repeat=num):
            print ', '.join(group) + ':',
            print ''.join(poss), counts[poss]

which produces:

PotA, PotB: ++ 3
PotA, PotB: +- 1
PotA, PotB: +? 0
PotA, PotB: -+ 0
PotA, PotB: -- 0
PotA, PotB: -? 1
PotA, PotB: ?+ 0
PotA, PotB: ?- 0
PotA, PotB: ?? 0
PotA, PotC: ++ 4
[...]
PotA, PotB, PotC, PotD, PotE: +++++ 3
PotA, PotB, PotC, PotD, PotE: ++++- 0
[...]

Note that I'm assuming that your desired output is in error, because in this line:

PotA, PotB, PotC, PotD, PotE: ++++++ 2

you have five columns on the left but six + symbols on the right.

like image 149
DSM Avatar answered Nov 10 '22 04:11

DSM