[Tutor] The Python way and two dimensional lists

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Nov 22 11:45:40 EST 2021


On Mon, 22 Nov 2021 17:02:34 +1100, Phil <phillor9 at gmail.com> declaimed the
following:

>
>solution[0][0] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, 
>{2}
>
>So row 0 is {7,3}, {5} etc
>
>and row 1 is {1} followed by 8 zeros
>
>for row in solution:
>     print(row[0])
>
>({3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2})

>
>Why do the sets represent only one position and the eight zeros make up 
>the remaining nine positions?
>
	Because that is what you told it to be... Note the () around the
printed value -- that indicates a tuple. Tuples don't need (), they need
the comma.

	You bound the nine-element tuple to the [0][0] element of your
list-of-lists. Python does not automatically unpack tuples unless you
provide enough destination spaces for it.

>>> solution[0][0:8] = {7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2}

	Here I provide 9 destination spaces for the nine elements on the right.

>>> print(solution[0])
[{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}, [2]]
>>> 

	And here is a LIST of nine elements as the result, and they are
individually accessible

>>> solution[0][1]
{5}
>>> 

	The other way is to provide a LIST on the right side

>>> solution2 = [0] * 9

	Create a nine element (column) list

>>> solution2
[0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> solution2[0] = [	{7,3}, {5}, {4,8,6}, {7,8}, {1}, {9,3}, {7,9}, {4,6,3}, {2}	]

	Replace the first element with the nine element (row) list

>>> solution2
[[{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}], 0,
0, 0, 0, 0, 0, 0, 0]
>>> solution2[0]
[{3, 7}, {5}, {8, 4, 6}, {8, 7}, {1}, {9, 3}, {9, 7}, {3, 4, 6}, {2}]
>>> solution2[0][1]
{5}
>>> 


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list