I feel like I’m doing something very dumb here. Maybe I’m just too tired.
I’m trying to make a truth table with a size depending on “count” – the number of variables for the table.
table = [[None] * int(pow(2, count))] * count
in_a_row = pow(2, count) / 2
iterator = 0
for i in range(count):
for j in range(int(pow(2, count))):
print(str(i) + ' ' + str(j), end = '')
if iterator < in_a_row:
table[i][j] = 'T'
print(' T')
elif iterator == 2 * in_a_row:
table[i][j] = 'T'
iterator = 0
print(' T')
else:
table[i][j] = 'F'
print(' F')
iterator += 1
print(table)
in_a_row /= 2
iterator = 0
Which outputs this:
0 0 T
0 1 T
0 2 F
0 3 F
[['T', 'T', 'F', 'F'], ['T', 'T', 'F', 'F']]
1 0 T
1 1 F
1 2 T
1 3 F
[['T', 'F', 'T', 'F'], ['T', 'F', 'T', 'F']]
You can see what whatever I set in one iteration is echoed in all “rows”. Can anyone show me what’s wrong here?
I’m of course expecting this:
[['T', 'T', 'F', 'F'], ['T', 'F', 'T', 'F']]
Change the first line to:
as you’ve no doubt noticed, in the first version, each row shares a reference to the same list, whereas in this new version, we create a new list for each row.