Python counts all possible combinations for a table

I have a table that looks like this:

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

From here I have to find all possible combinations of "+", "-" and "?". for all combinations (PotA and PotB), (PotA and PotC), etc. (PotA, PotB and PotC) and, finally, to (PotA, PotB, PotC, PotD and PotE). In fact, the line "Pot" continues to continue, but here I will only show PotE for simplicity.

To do this, first read the file as described below, and then create all the possible possibilities for a combination of the two to 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 will 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 a good python method for the correct logic for this problem? Should I read heading data in the form of keys and columns as a list value?

I can not get the correct logic. Please help me.

+8
python matrix permutation combinations
source share
1 answer

Assuming I understand what you need, what 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 [...] 

Please note that I assume that your desired result is erroneous because on this line:

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

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

+16
source share

All Articles