[Tutor] The Python way and two dimensional lists
Phil
phillor9 at gmail.com
Sun Nov 21 19:13:15 EST 2021
I hope my questions are not becoming too tiresome.
It will require a major effort to rewrite my use of lists in the Python
way and so I want to make sure that the first steps are correct. The
first index refers to the columns and the second to the rows.
num_rows = 9
num_cols = 9
solution = [[0 for i in range(num_cols)] for j in range(num_rows)]
solution[0][0] = {7,3}
solution[1][0] = {5}
solution[2][0] = {4,8,6}
solution[3][0] = {7,8}
solution[4][0] = {1}
solution[5][0] = {9,3}
solution[6][0] = {7,9}
solution[7][0] = {4,6,3}
solution[8][0] = {2}
solution[0][1] = {1,2,3}
#print(solution)
for col in solution:
print(col[0])
print()
for row in solution:
print(row[1])
If I want to refer to column 6, is it the Python way to say x = col[6]?
Also regarding enumeration. The following is a slight modification to
Mats code and it gives me exactly what I want:
row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}]
enum_row = enumerate(row)
row_position = []
for index, enum_row in enumerate(row):
print('enum_row ',enum_row )
if i in enum_row:#r:
icount += 1
row_position.append(index)
Now returning to the two dimensional list above and assuming that col[0]
is referring to a row (just for this question only). I'm not sure if
this is correct because of an error in another section of the code
enum_row = enumerate(col[0]) # instead or row
for index, enum_row in enumerate(col[0]): # instead or row
print('enum_row ',enum_row )
if i in enum_row:
icount += 1
This is where the error is occurring (also from Mats code):
for s in set_list:
for r in col[0]: # instead of row:
if s.issubset(r):
pairs[tuple(sorted(s))] += 1
Traceback (most recent call last):
File "/usr/lib/python3.9/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "/home/phil/Python/set_count2.py", line 75, in <module>
if s.issubset(r):
TypeError: 'int' object is not iterable
Confusion is setting in. I think my use of enumerate is correct and I
think the way that I'm referencing a row from the solution list is
correct. If both of those aren't the source of the error then it has to
do with my use of sets. for r in col[0] doesn't give me the sets
{7,3},{5} etc.
Searching the Internet for information hasn't made me any wiser yet and
again I'm sorry to burden the list members with my problems.
--
Regards,
Phil
More information about the Tutor
mailing list