0

I have this list and number:

list = ['B','C']

The outcome that I need for my table is:

B    C    Prob
0    0    x
0    1    x
1    0    x
1    1    x

How can I build this truth table (there can be more vairables, not only 3) and assign a number to that row's probability?

I need to build it with a dictionary, I tried with some list comprehension but I don't know how to generate dynamically the truth table, considering that there can be more/less than 3 variables.

EDIT: to be more clear my goal is to have a dictionary like this:

dict = {"B":0/1,"C":0/1,"Prob":arbitraryNumber}

and I need to insert all these dictionaries into a list to represent the structure of a table, is it clearer now?

Thank you very much

4
  • Follow-up of stackoverflow.com/questions/23516212/… :) Commented May 7, 2014 at 11:35
  • @The Condor, how are you calculating the probability? Commented May 7, 2014 at 12:13
  • @PadraicCunningham I have some data from a XML file, no calculation needed Commented May 7, 2014 at 12:47
  • Question edited, please take a look at it ;) Commented May 7, 2014 at 14:55

2 Answers 2

1

You can generate the truth table using a powerset,

def power_set(items):
    n = len(items)
    for i in xrange(2**n):
        combo = []
        for j in xrange(n):
            if (i >> j) % 2 == 1:
                combo.append(1)
            else:
                combo.append(0)
        yield combo    # if you want tuples, change to yield tuple(combo)


In [13]: list(power_set(l))
Out[13]: [[0, 0], [1, 0], [0, 1], [1, 1]]

In [14]: l=['B','C','E']

In [15]: list(power_set(l))
Out[15]: 
[[0, 0, 0],
[1, 0, 0],
 [0, 1, 0],
 [1, 1, 0],
 [0, 0, 1],
 [1, 0, 1],
 [0, 1, 1],
 [1, 1, 1]]

If you want to make a dict of the data, change yield combo to yield tuple(combo)

Then you can store key value pairings like:

d={}
for data in power_set(l):
    d[data]="your_calc_prob"
print d
{(0, 1): 'your_calc_prob', (1, 0): 'your_calc_prob', (0, 0): 'your_calc_prob', (1, 1): 'your_calc_prob'}

If you want the output sorted you can use sorted() which makes a copy of the list and returns a list:

 sorted(list(power_set(l)))
 Out[21]: 
 [[0, 0, 0],
 [0, 0, 1],
 [0, 1, 0],
 [0, 1, 1],
 [1, 0, 0],
 [1, 0, 1],
 [1, 1, 0],
 [1, 1, 1]]

Or you can use the list method sort() which sorts the list in place:

In [22]: data = list(power_set(l))  
In [23]: data.sort()
In [24]: data
Out[24]: 
[[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]]
Sign up to request clarification or add additional context in comments.

6 Comments

Cool! Perfect except for one thing: the numbers I get are "specular", I mean for instance that the second line should be 001 and not 100, can you explain me how to do it?
Do you mean the output should be reversed?
I don't know the exact meaning of reverse in matrix speaking, but for instance I need [000,001,010,011,100,101,110,111], is it more clear now?
Ah ok i understand. I will edit my answers when i get back on my computer
No problem.you should probably edit your question to reflect the output that you need.
|
0

You can use itertools.product() to generate the truth table and then depending on the logical operation, determine the probability. I don't know which logical operation you would like to use so let's just create a dictionary each row:

>>> l = ['B', 'C']
>>> truth_table = [dict(zip(l, x)) for x in product((0, 1), repeat=2)]
>>> print(truth_table)
[{'B': 0, 'C': 0}, {'B': 0, 'C': 1}, {'B': 1, 'C': 0}, {'B': 1, 'C': 1}]

For calculating the probability, you'll probably need a separate function to do that. For example a logical disjunction for two keys with 0 and 1 being the values is basically equivalent to max().

>>> l.append('Prob')
>>> truth_table = [dict(zip(l, x + (max(x), )) for x in product((0, 1), repeat=2)]
>>> print(truth_table)
[{'B': 0, 'C': 0, 'Prob': 0},
 {'B': 0, 'C': 1, 'Prob': 1},
 {'B': 1, 'C': 0, 'Prob': 1},
 {'B': 1, 'C': 1, 'Prob': 1}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.